1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 Ventana Micro Systems Inc.
*
* Authors:
* Subrahmanya Lingappa <slingappa@ventanamicro.com>
*/
#include <libfdt.h>
#include <sbi/sbi_console.h>
#include <sbi/sbi_heap.h>
#include <sbi/sbi_hsm.h>
#include <sbi/sbi_scratch.h>
#include <sbi_utils/fdt/fdt_driver.h>
#include <sbi_utils/fdt/fdt_fixup.h>
#include <sbi_utils/fdt/fdt_helper.h>
#include <sbi_utils/mailbox/fdt_mailbox.h>
#include <sbi_utils/mailbox/mailbox.h>
#include <sbi_utils/mailbox/rpmi_mailbox.h>
#define MAX_HSM_SUPSEND_STATE_NAMELEN 16
struct rpmi_hsm_suspend {
u32 num_states;
struct sbi_cpu_idle_state *states;
};
struct rpmi_hsm {
struct mbox_chan *chan;
struct rpmi_hsm_suspend *susp;
};
static unsigned long rpmi_hsm_offset;
static struct rpmi_hsm *rpmi_hsm_get_pointer(u32 hartid)
{
struct sbi_scratch *scratch;
scratch = sbi_hartid_to_scratch(hartid);
if (!scratch || !rpmi_hsm_offset)
return NULL;
return sbi_scratch_offset_ptr(scratch, rpmi_hsm_offset);
}
static int rpmi_hsm_start(u32 hartid, ulong resume_addr)
{
struct rpmi_hsm_hart_start_req req;
struct rpmi_hsm_hart_start_resp resp;
struct rpmi_hsm *rpmi = rpmi_hsm_get_pointer(hartid);
if (!rpmi)
return SBI_ENOSYS;
req.hartid = hartid;
req.start_addr_lo = resume_addr;
req.start_addr_hi = (u64)resume_addr >> 32;
return rpmi_normal_request_with_status(
rpmi->chan, RPMI_HSM_SRV_HART_START,
&req, rpmi_u32_count(req), rpmi_u32_count(req),
&resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
}
static int rpmi_hsm_stop(void)
{
int rc;
struct rpmi_hsm_hart_stop_req req;
struct rpmi_hsm_hart_stop_resp resp;
void (*jump_warmboot)(void) =
(void (*)(void))sbi_scratch_thishart_ptr()->warmboot_addr;
struct rpmi_hsm *rpmi = rpmi_hsm_get_pointer(current_hartid());
if (!rpmi)
return SBI_ENOSYS;
req.hartid = current_hartid();
rc = rpmi_normal_request_with_status(
rpmi->chan, RPMI_HSM_SRV_HART_STOP,
&req, rpmi_u32_count(req), rpmi_u32_count(req),
&resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
if (rc)
return rc;
/* Wait for interrupt */
wfi();
jump_warmboot();
return 0;
}
static bool is_rpmi_hsm_susp_supported(struct rpmi_hsm_suspend *susp, u32 type)
{
int i;
for (i = 0; i < susp->num_states; i++)
if (type == susp->states[i].suspend_param)
return true;
return false;
}
static int rpmi_hsm_suspend(u32 type, ulong resume_addr)
{
int rc;
struct rpmi_hsm_hart_susp_req req;
struct rpmi_hsm_hart_susp_resp resp;
struct rpmi_hsm *rpmi = rpmi_hsm_get_pointer(current_hartid());
if (!rpmi)
return SBI_ENOSYS;
/* check if harts support this suspend type */
if (!is_rpmi_hsm_susp_supported(rpmi->susp, type))
return SBI_EINVAL;
req.hartid = current_hartid();
req.suspend_type = type;
req.resume_addr_lo = resume_addr;
req.resume_addr_hi = (u64)resume_addr >> 32;
rc = rpmi_normal_request_with_status(
rpmi->chan, RPMI_HSM_SRV_HART_SUSPEND,
&req, rpmi_u32_count(req), rpmi_u32_count(req),
&resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
if (rc)
return rc;
/* Wait for interrupt */
wfi();
return 0;
}
static struct sbi_hsm_device sbi_hsm_rpmi = {
.name = "rpmi-hsm",
.hart_start = rpmi_hsm_start,
.hart_stop = rpmi_hsm_stop,
.hart_suspend = rpmi_hsm_suspend,
};
static void rpmi_hsm_do_fixup(struct fdt_general_fixup *f, void *fdt)
{
struct rpmi_hsm *rpmi = rpmi_hsm_get_pointer(current_hartid());
if (!rpmi || !rpmi->susp || !rpmi->susp->num_states)
return;
fdt_add_cpu_idle_states(fdt, rpmi->susp->states);
}
static struct fdt_general_fixup rpmi_hsm_fixup = {
.name = "rpmi-hsm-fixup",
.do_fixup = rpmi_hsm_do_fixup,
};
static int rpmi_hsm_get_num_suspend_states(struct mbox_chan *chan,
struct rpmi_hsm_suspend *susp)
{
int rc;
struct rpmi_hsm_get_susp_types_req req;
struct rpmi_hsm_get_susp_types_resp resp;
req.start_index = 0;
rc = rpmi_normal_request_with_status(
chan, RPMI_HSM_SRV_GET_SUSPEND_TYPES,
&req, rpmi_u32_count(req), rpmi_u32_count(req),
&resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
if (rc)
return rc;
susp->num_states = resp.returned + resp.remaining;
return 0;
}
static int rpmi_hsm_get_suspend_states(struct mbox_chan *chan,
struct rpmi_hsm_suspend *susp)
{
int rc, i, cnt = 0;
struct rpmi_hsm_get_susp_types_req req;
struct rpmi_hsm_get_susp_types_resp resp;
struct rpmi_hsm_get_susp_info_req dreq;
struct rpmi_hsm_get_susp_info_resp dresp;
struct sbi_cpu_idle_state *state;
if (!susp->num_states)
return 0;
req.start_index = 0;
do {
rc = rpmi_normal_request_with_status(
chan, RPMI_HSM_SRV_GET_SUSPEND_TYPES,
&req, rpmi_u32_count(req), rpmi_u32_count(req),
&resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
if (rc)
return rc;
for (i = 0; i < resp.returned && cnt < susp->num_states; i++)
susp->states[cnt++].suspend_param = resp.types[i];
req.start_index = i;
} while (resp.remaining);
for (i = 0; i < susp->num_states; i++) {
state = &susp->states[i];
dreq.suspend_type = state->suspend_param;
rc = rpmi_normal_request_with_status(
chan, RPMI_HSM_SRV_GET_SUSPEND_INFO,
&dreq, rpmi_u32_count(dreq), rpmi_u32_count(dreq),
&dresp, rpmi_u32_count(dresp), rpmi_u32_count(dresp));
if (rc)
return rc;
state->local_timer_stop =
(dresp.flags & RPMI_HSM_SUSPEND_INFO_FLAGS_TIMER_STOP) ? true : false;
state->entry_latency_us = dresp.entry_latency_us;
state->exit_latency_us = dresp.exit_latency_us;
state->wakeup_latency_us = dresp.wakeup_latency_us;
state->min_residency_us = dresp.min_residency_us;
}
return 0;
}
static int rpmi_hsm_update_hart_scratch(struct mbox_chan *chan,
struct rpmi_hsm_suspend *susp)
{
int rc, i;
struct rpmi_hsm_get_hart_list_req req;
struct rpmi_hsm_get_hart_list_resp resp;
struct rpmi_hsm *rpmi = rpmi_hsm_get_pointer(current_hartid());
req.start_index = 0;
do {
rc = rpmi_normal_request_with_status(
chan, RPMI_HSM_SRV_GET_HART_LIST,
&req, rpmi_u32_count(req), rpmi_u32_count(req),
&resp, rpmi_u32_count(resp), rpmi_u32_count(resp));
if (rc)
return rc;
for (i = 0; i < resp.returned; i++) {
rpmi = rpmi_hsm_get_pointer(resp.hartid[i]);
if (!rpmi)
return SBI_ENOSYS;
rpmi->chan = chan;
rpmi->susp = susp;
}
req.start_index += resp.returned;
} while (resp.remaining);
return 0;
}
static int rpmi_hsm_cold_init(const void *fdt, int nodeoff,
const struct fdt_match *match)
{
int rc, i;
struct mbox_chan *chan;
struct rpmi_hsm_suspend *susp;
if (!rpmi_hsm_offset) {
rpmi_hsm_offset =
sbi_scratch_alloc_type_offset(struct rpmi_hsm);
if (!rpmi_hsm_offset)
return SBI_ENOMEM;
}
/*
* If channel request failed then other end does not support
* HSM service group so do nothing.
*/
rc = fdt_mailbox_request_chan(fdt, nodeoff, 0, &chan);
if (rc)
return SBI_ENODEV;
/* Allocate context for HART suspend states */
susp = sbi_zalloc(sizeof(*susp));
if (!susp)
return SBI_ENOMEM;
/* Get number of HART suspend states */
rc = rpmi_hsm_get_num_suspend_states(chan, susp);
if (rc)
goto fail_free_susp;
/* Skip HART suspend state discovery for zero HART suspend states */
if (!susp->num_states)
goto skip_suspend_states;
/* Allocate array of HART suspend states */
susp->states = sbi_calloc(susp->num_states + 1, sizeof(*susp->states));
if (!susp->states) {
rc = SBI_ENOMEM;
goto fail_free_susp;
}
/* Allocate name of each HART suspend state */
for (i = 0; i < susp->num_states; i++) {
susp->states[i].name =
sbi_zalloc(MAX_HSM_SUPSEND_STATE_NAMELEN);
if (!susp->states[i].name) {
do {
i--;
sbi_free((void *)susp->states[i].name);
} while (i > 0);
rc = SBI_ENOMEM;
goto fail_free_susp_states;
}
sbi_snprintf((char *)susp->states[i].name,
MAX_HSM_SUPSEND_STATE_NAMELEN, "cpu-susp%d", i);
}
/* Get details about each HART suspend state */
rc = rpmi_hsm_get_suspend_states(chan, susp);
if (rc)
goto fail_free_susp_state_names;
skip_suspend_states:
/* Update per-HART scratch space */
rc = rpmi_hsm_update_hart_scratch(chan, susp);
if (rc)
goto fail_free_susp_state_names;
/* Register HSM fixup callback */
rc = fdt_register_general_fixup(&rpmi_hsm_fixup);
if (rc)
goto fail_free_susp_state_names;
/* Register HSM device */
if (!susp->num_states)
sbi_hsm_rpmi.hart_suspend = NULL;
sbi_hsm_set_device(&sbi_hsm_rpmi);
return 0;
fail_free_susp_state_names:
for (i = 0; i < susp->num_states; i++)
sbi_free((void *)susp->states[i].name);
fail_free_susp_states:
if (susp->num_states)
sbi_free(susp->states);
fail_free_susp:
sbi_free(susp);
return rc;
}
static const struct fdt_match rpmi_hsm_match[] = {
{ .compatible = "riscv,rpmi-hsm" },
{},
};
const struct fdt_driver fdt_hsm_rpmi = {
.match_table = rpmi_hsm_match,
.init = rpmi_hsm_cold_init,
.experimental = true,
};
|