sdei_intr_mgmt.c 20.7 KB
Newer Older
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
1
/*
2
 * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
3
4
5
6
7
8
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <string.h>
9
10

#include <arch_helpers.h>
11
#include <arch_features.h>
12
13
14
15
16
17
18
19
#include <bl31/ehf.h>
#include <bl31/interrupt_mgmt.h>
#include <common/bl_common.h>
#include <common/debug.h>
#include <common/runtime_svc.h>
#include <lib/cassert.h>
#include <services/sdei.h>

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
20
21
22
#include "sdei_private.h"

/* x0-x17 GPREGS context */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
23
#define SDEI_SAVED_GPREGS	18U
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
24
25

/* Maximum preemption nesting levels: Critical priority and Normal priority */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
26
#define MAX_EVENT_NESTING	2U
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
27
28

/* Per-CPU SDEI state access macro */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
29
#define sdei_get_this_pe_state()	(&cpu_state[plat_my_core_pos()])
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
30
31
32
33
34

/* Structure to store information about an outstanding dispatch */
typedef struct sdei_dispatch_context {
	sdei_ev_map_t *map;
	uint64_t x[SDEI_SAVED_GPREGS];
35
	jmp_buf *dispatch_jmp;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
36
37
38
39

	/* Exception state registers */
	uint64_t elr_el3;
	uint64_t spsr_el3;
40
41
42
43
44

#if DYNAMIC_WORKAROUND_CVE_2018_3639
	/* CVE-2018-3639 mitigation state */
	uint64_t disable_cve_2018_3639;
#endif
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
45
46
47
48
49
50
} sdei_dispatch_context_t;

/* Per-CPU SDEI state data */
typedef struct sdei_cpu_state {
	sdei_dispatch_context_t dispatch_stack[MAX_EVENT_NESTING];
	unsigned short stack_top; /* Empty ascending */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
51
52
	bool pe_masked;
	bool pending_enables;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
53
54
55
} sdei_cpu_state_t;

/* SDEI states for all cores in the system */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
56
static sdei_cpu_state_t cpu_state[PLATFORM_CORE_COUNT];
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
57

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
58
int64_t sdei_pe_mask(void)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
59
{
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
60
	int64_t ret = 0;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
61
62
63
64
65
66
	sdei_cpu_state_t *state = sdei_get_this_pe_state();

	/*
	 * Return value indicates whether this call had any effect in the mask
	 * status of this PE.
	 */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
67
68
69
70
	if (!state->pe_masked) {
		state->pe_masked = true;
		ret = 1;
	}
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
71
72
73
74
75
76

	return ret;
}

void sdei_pe_unmask(void)
{
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
77
	unsigned int i;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	sdei_ev_map_t *map;
	sdei_entry_t *se;
	sdei_cpu_state_t *state = sdei_get_this_pe_state();
	uint64_t my_mpidr = read_mpidr_el1() & MPIDR_AFFINITY_MASK;

	/*
	 * If there are pending enables, iterate through the private mappings
	 * and enable those bound maps that are in enabled state. Also, iterate
	 * through shared mappings and enable interrupts of events that are
	 * targeted to this PE.
	 */
	if (state->pending_enables) {
		for_each_private_map(i, map) {
			se = get_event_entry(map);
			if (is_map_bound(map) && GET_EV_STATE(se, ENABLED))
				plat_ic_enable_interrupt(map->intr);
		}

		for_each_shared_map(i, map) {
			se = get_event_entry(map);

			sdei_map_lock(map);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
100
			if (is_map_bound(map) && GET_EV_STATE(se, ENABLED) &&
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
101
102
103
104
105
106
107
108
					(se->reg_flags == SDEI_REGF_RM_PE) &&
					(se->affinity == my_mpidr)) {
				plat_ic_enable_interrupt(map->intr);
			}
			sdei_map_unlock(map);
		}
	}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
109
110
	state->pending_enables = false;
	state->pe_masked = false;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
}

/* Push a dispatch context to the dispatch stack */
static sdei_dispatch_context_t *push_dispatch(void)
{
	sdei_cpu_state_t *state = sdei_get_this_pe_state();
	sdei_dispatch_context_t *disp_ctx;

	/* Cannot have more than max events */
	assert(state->stack_top < MAX_EVENT_NESTING);

	disp_ctx = &state->dispatch_stack[state->stack_top];
	state->stack_top++;

	return disp_ctx;
}

/* Pop a dispatch context to the dispatch stack */
static sdei_dispatch_context_t *pop_dispatch(void)
{
	sdei_cpu_state_t *state = sdei_get_this_pe_state();

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
133
	if (state->stack_top == 0U)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
		return NULL;

	assert(state->stack_top <= MAX_EVENT_NESTING);

	state->stack_top--;

	return &state->dispatch_stack[state->stack_top];
}

/* Retrieve the context at the top of dispatch stack */
static sdei_dispatch_context_t *get_outstanding_dispatch(void)
{
	sdei_cpu_state_t *state = sdei_get_this_pe_state();

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
148
	if (state->stack_top == 0U)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
149
150
151
152
		return NULL;

	assert(state->stack_top <= MAX_EVENT_NESTING);

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
153
	return &state->dispatch_stack[state->stack_top - 1U];
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
154
155
}

156
157
static sdei_dispatch_context_t *save_event_ctx(sdei_ev_map_t *map,
		void *tgt_ctx)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
158
159
{
	sdei_dispatch_context_t *disp_ctx;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
160
161
	const gp_regs_t *tgt_gpregs;
	const el3_state_t *tgt_el3;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
162

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
163
	assert(tgt_ctx != NULL);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
164
165
166
167
	tgt_gpregs = get_gpregs_ctx(tgt_ctx);
	tgt_el3 = get_el3state_ctx(tgt_ctx);

	disp_ctx = push_dispatch();
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
168
	assert(disp_ctx != NULL);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
169
170
171
172
173
174
	disp_ctx->map = map;

	/* Save general purpose and exception registers */
	memcpy(disp_ctx->x, tgt_gpregs, sizeof(disp_ctx->x));
	disp_ctx->spsr_el3 = read_ctx_reg(tgt_el3, CTX_SPSR_EL3);
	disp_ctx->elr_el3 = read_ctx_reg(tgt_el3, CTX_ELR_EL3);
175

176
	return disp_ctx;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
177
178
}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
179
static void restore_event_ctx(const sdei_dispatch_context_t *disp_ctx, void *tgt_ctx)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
180
181
182
183
{
	gp_regs_t *tgt_gpregs;
	el3_state_t *tgt_el3;

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
184
	assert(tgt_ctx != NULL);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
185
186
187
188
189
190
191
192
193
194
	tgt_gpregs = get_gpregs_ctx(tgt_ctx);
	tgt_el3 = get_el3state_ctx(tgt_ctx);

	CASSERT(sizeof(disp_ctx->x) == (SDEI_SAVED_GPREGS * sizeof(uint64_t)),
			foo);

	/* Restore general purpose and exception registers */
	memcpy(tgt_gpregs, disp_ctx->x, sizeof(disp_ctx->x));
	write_ctx_reg(tgt_el3, CTX_SPSR_EL3, disp_ctx->spsr_el3);
	write_ctx_reg(tgt_el3, CTX_ELR_EL3, disp_ctx->elr_el3);
195
196
197
198
199
200
201
202
203

#if DYNAMIC_WORKAROUND_CVE_2018_3639
	cve_2018_3639_t *tgt_cve_2018_3639;
	tgt_cve_2018_3639 = get_cve_2018_3639_ctx(tgt_ctx);

	/* Restore CVE-2018-3639 mitigation state */
	write_ctx_reg(tgt_cve_2018_3639, CTX_CVE_2018_3639_DISABLE,
		disp_ctx->disable_cve_2018_3639);
#endif
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
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
}

static void save_secure_context(void)
{
	cm_el1_sysregs_context_save(SECURE);
}

/* Restore Secure context and arrange to resume it at the next ERET */
static void restore_and_resume_secure_context(void)
{
	cm_el1_sysregs_context_restore(SECURE);
	cm_set_next_eret_context(SECURE);
}

/*
 * Restore Non-secure context and arrange to resume it at the next ERET. Return
 * pointer to the Non-secure context.
 */
static cpu_context_t *restore_and_resume_ns_context(void)
{
	cpu_context_t *ns_ctx;

	cm_el1_sysregs_context_restore(NON_SECURE);
	cm_set_next_eret_context(NON_SECURE);

	ns_ctx = cm_get_context(NON_SECURE);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
230
	assert(ns_ctx != NULL);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
231
232
233
234

	return ns_ctx;
}

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
/*
 * Prepare for ERET:
 * - Set the ELR to the registered handler address
 * - Set the SPSR register as described in the SDEI documentation and
 *   the AArch64.TakeException() pseudocode function in
 *   ARM DDI 0487F.c page J1-7635
 */

static void sdei_set_elr_spsr(sdei_entry_t *se, sdei_dispatch_context_t *disp_ctx)
{
	unsigned int client_el = sdei_client_el();
	u_register_t sdei_spsr = SPSR_64(client_el, MODE_SP_ELX,
					DISABLE_ALL_EXCEPTIONS);

	u_register_t interrupted_pstate = disp_ctx->spsr_el3;

	/* Check the SPAN bit in the client el SCTLR */
	u_register_t client_el_sctlr;

	if (client_el == MODE_EL2) {
		client_el_sctlr = read_sctlr_el2();
	} else {
		client_el_sctlr = read_sctlr_el1();
	}

	/*
	 * Check whether to force the PAN bit or use the value in the
	 * interrupted EL according to the check described in
	 * TakeException. Since the client can only be Non-Secure
	 * EL2 or El1 some of the conditions in ElIsInHost() we know
	 * will always be True.
	 * When the client_el is EL2 we know that there will be a SPAN
	 * bit in SCTLR_EL2 as we have already checked for the condition
	 * HCR_EL2.E2H = 1 and HCR_EL2.TGE = 1
	 */
	u_register_t hcr_el2 = read_hcr();
	bool el_is_in_host = is_armv8_1_vhe_present() &&
			     (hcr_el2 & HCR_TGE_BIT) &&
			     (hcr_el2 & HCR_E2H_BIT);

	if (is_armv8_1_pan_present() &&
	    ((client_el == MODE_EL1) ||
		(client_el == MODE_EL2 && el_is_in_host)) &&
	    ((client_el_sctlr & SCTLR_SPAN_BIT) == 0U)) {
		sdei_spsr |=  SPSR_PAN_BIT;
	} else {
		sdei_spsr |= (interrupted_pstate & SPSR_PAN_BIT);
	}

	/* If SSBS is implemented, take the value from the client el SCTLR */
	u_register_t ssbs_enabled = (read_id_aa64pfr1_el1()
					>> ID_AA64PFR1_EL1_SSBS_SHIFT)
					& ID_AA64PFR1_EL1_SSBS_MASK;
	if (ssbs_enabled != SSBS_UNAVAILABLE) {
		u_register_t  ssbs_bit = ((client_el_sctlr & SCTLR_DSSBS_BIT)
						>> SCTLR_DSSBS_SHIFT)
						<< SPSR_SSBS_SHIFT_AARCH64;
		sdei_spsr |= ssbs_bit;
	}

	/* If MTE is implemented in the client el set the TCO bit */
	if (get_armv8_5_mte_support() >= MTE_IMPLEMENTED_ELX) {
		sdei_spsr |= SPSR_TCO_BIT_AARCH64;
	}

	/* Take the DIT field from the pstate of the interrupted el */
	sdei_spsr |= (interrupted_pstate & SPSR_DIT_BIT);

	cm_set_elr_spsr_el3(NON_SECURE, (uintptr_t) se->ep, sdei_spsr);
}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
306
307
308
309
310
/*
 * Populate the Non-secure context so that the next ERET will dispatch to the
 * SDEI client.
 */
static void setup_ns_dispatch(sdei_ev_map_t *map, sdei_entry_t *se,
311
		cpu_context_t *ctx, jmp_buf *dispatch_jmp)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
312
{
313
	sdei_dispatch_context_t *disp_ctx;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
314
315

	/* Push the event and context */
316
	disp_ctx = save_event_ctx(map, ctx);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
317
318
319
320
321
322
323
324
325

	/*
	 * Setup handler arguments:
	 *
	 * - x0: Event number
	 * - x1: Handler argument supplied at the time of event registration
	 * - x2: Interrupted PC
	 * - x3: Interrupted SPSR
	 */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
326
	SMC_SET_GP(ctx, CTX_GPREG_X0, (uint64_t) map->ev_num);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
327
	SMC_SET_GP(ctx, CTX_GPREG_X1, se->arg);
328
329
	SMC_SET_GP(ctx, CTX_GPREG_X2, disp_ctx->elr_el3);
	SMC_SET_GP(ctx, CTX_GPREG_X3, disp_ctx->spsr_el3);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
330

331
332
	/* Setup the elr and spsr register to prepare for ERET */
	sdei_set_elr_spsr(se, disp_ctx);
333
334
335
336
337
338
339
340
341
342
343
344
345
346

#if DYNAMIC_WORKAROUND_CVE_2018_3639
	cve_2018_3639_t *tgt_cve_2018_3639;
	tgt_cve_2018_3639 = get_cve_2018_3639_ctx(ctx);

	/* Save CVE-2018-3639 mitigation state */
	disp_ctx->disable_cve_2018_3639 = read_ctx_reg(tgt_cve_2018_3639,
		CTX_CVE_2018_3639_DISABLE);

	/* Force SDEI handler to execute with mitigation enabled by default */
	write_ctx_reg(tgt_cve_2018_3639, CTX_CVE_2018_3639_DISABLE, 0);
#endif

	disp_ctx->dispatch_jmp = dispatch_jmp;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
347
348
349
350
351
352
353
}

/* Handle a triggered SDEI interrupt while events were masked on this PE */
static void handle_masked_trigger(sdei_ev_map_t *map, sdei_entry_t *se,
		sdei_cpu_state_t *state, unsigned int intr_raw)
{
	uint64_t my_mpidr __unused = (read_mpidr_el1() & MPIDR_AFFINITY_MASK);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
354
	bool disable = false;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
355
356
357
358
359
360
361
362
363
364

	/* Nothing to do for event 0 */
	if (map->ev_num == SDEI_EVENT_0)
		return;

	/*
	 * For a private event, or for a shared event specifically routed to
	 * this CPU, we disable interrupt, leave the interrupt pending, and do
	 * EOI.
	 */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
365
366
367
368
	if (is_event_private(map) || (se->reg_flags == SDEI_REGF_RM_PE))
		disable = true;

	if (se->reg_flags == SDEI_REGF_RM_PE)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
369
370
371
372
373
374
		assert(se->affinity == my_mpidr);

	if (disable) {
		plat_ic_disable_interrupt(map->intr);
		plat_ic_set_interrupt_pending(map->intr);
		plat_ic_end_of_interrupt(intr_raw);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
375
		state->pending_enables = true;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
376
377
378
379
380
381
382
383
384
385
386
387

		return;
	}

	/*
	 * We just received a shared event with routing set to ANY PE. The
	 * interrupt can't be delegated on this PE as SDEI events are masked.
	 * However, because its routing mode is ANY, it is possible that the
	 * event can be delegated on any other PE that hasn't masked events.
	 * Therefore, we set the interrupt back pending so as to give other
	 * suitable PEs a chance of handling it.
	 */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
388
	assert(plat_ic_is_spi(map->intr) != 0);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
	plat_ic_set_interrupt_pending(map->intr);

	/*
	 * Leaving the same interrupt pending also means that the same interrupt
	 * can target this PE again as soon as this PE leaves EL3. Whether and
	 * how often that happens depends on the implementation of GIC.
	 *
	 * We therefore call a platform handler to resolve this situation.
	 */
	plat_sdei_handle_masked_trigger(my_mpidr, map->intr);

	/* This PE is masked. We EOI the interrupt, as it can't be delegated */
	plat_ic_end_of_interrupt(intr_raw);
}

/* SDEI main interrupt handler */
int sdei_intr_handler(uint32_t intr_raw, uint32_t flags, void *handle,
		void *cookie)
{
	sdei_entry_t *se;
	cpu_context_t *ctx;
	sdei_ev_map_t *map;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
411
	const sdei_dispatch_context_t *disp_ctx;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
412
413
414
	unsigned int sec_state;
	sdei_cpu_state_t *state;
	uint32_t intr;
415
	jmp_buf dispatch_jmp;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
416
	const uint64_t mpidr = read_mpidr_el1();
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

	/*
	 * To handle an event, the following conditions must be true:
	 *
	 * 1. Event must be signalled
	 * 2. Event must be enabled
	 * 3. This PE must be a target PE for the event
	 * 4. PE must be unmasked for SDEI
	 * 5. If this is a normal event, no event must be running
	 * 6. If this is a critical event, no critical event must be running
	 *
	 * (1) and (2) are true when this function is running
	 * (3) is enforced in GIC by selecting the appropriate routing option
	 * (4) is satisfied by client calling PE_UNMASK
	 * (5) and (6) is enforced using interrupt priority, the RPR, in GIC:
	 *   - Normal SDEI events belong to Normal SDE priority class
	 *   - Critical SDEI events belong to Critical CSDE priority class
	 *
	 * The interrupt has already been acknowledged, and therefore is active,
	 * so no other PE can handle this event while we are at it.
	 *
	 * Find if this is an SDEI interrupt. There must be an event mapped to
	 * this interrupt
	 */
	intr = plat_ic_get_interrupt_id(intr_raw);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
442
443
	map = find_event_map_by_intr(intr, (plat_ic_is_spi(intr) != 0));
	if (map == NULL) {
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
444
445
446
447
448
449
450
451
452
453
454
455
456
		ERROR("No SDEI map for interrupt %u\n", intr);
		panic();
	}

	/*
	 * Received interrupt number must either correspond to event 0, or must
	 * be bound interrupt.
	 */
	assert((map->ev_num == SDEI_EVENT_0) || is_map_bound(map));

	se = get_event_entry(map);
	state = sdei_get_this_pe_state();

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
457
	if (state->pe_masked) {
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
458
459
460
461
		/*
		 * Interrupts received while this PE was masked can't be
		 * dispatched.
		 */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
462
463
		SDEI_LOG("interrupt %u on %llx while PE masked\n", map->intr,
				mpidr);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
		if (is_event_shared(map))
			sdei_map_lock(map);

		handle_masked_trigger(map, se, state, intr_raw);

		if (is_event_shared(map))
			sdei_map_unlock(map);

		return 0;
	}

	/* Insert load barrier for signalled SDEI event */
	if (map->ev_num == SDEI_EVENT_0)
		dmbld();

	if (is_event_shared(map))
		sdei_map_lock(map);

	/* Assert shared event routed to this PE had been configured so */
	if (is_event_shared(map) && (se->reg_flags == SDEI_REGF_RM_PE)) {
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
484
		assert(se->affinity == (mpidr & MPIDR_AFFINITY_MASK));
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
	}

	if (!can_sdei_state_trans(se, DO_DISPATCH)) {
		SDEI_LOG("SDEI event 0x%x can't be dispatched; state=0x%x\n",
				map->ev_num, se->state);

		/*
		 * If the event is registered, leave the interrupt pending so
		 * that it's delivered when the event is enabled.
		 */
		if (GET_EV_STATE(se, REGISTERED))
			plat_ic_set_interrupt_pending(map->intr);

		/*
		 * The interrupt was disabled or unregistered after the handler
		 * started to execute, which means now the interrupt is already
		 * disabled and we just need to EOI the interrupt.
		 */
		plat_ic_end_of_interrupt(intr_raw);

		if (is_event_shared(map))
			sdei_map_unlock(map);

		return 0;
	}

	disp_ctx = get_outstanding_dispatch();
	if (is_event_critical(map)) {
		/*
		 * If this event is Critical, and if there's an outstanding
		 * dispatch, assert the latter is a Normal dispatch. Critical
		 * events can preempt an outstanding Normal event dispatch.
		 */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
518
		if (disp_ctx != NULL)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
			assert(is_event_normal(disp_ctx->map));
	} else {
		/*
		 * If this event is Normal, assert that there are no outstanding
		 * dispatches. Normal events can't preempt any outstanding event
		 * dispatches.
		 */
		assert(disp_ctx == NULL);
	}

	sec_state = get_interrupt_src_ss(flags);

	if (is_event_shared(map))
		sdei_map_unlock(map);

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
534
535
	SDEI_LOG("ACK %llx, ev:%d ss:%d spsr:%lx ELR:%lx\n", mpidr, map->ev_num,
			sec_state, read_spsr_el3(), read_elr_el3());
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
536
537
538
539
540
541
542
543
544
545
546
547

	ctx = handle;

	/*
	 * Check if we interrupted secure state. Perform a context switch so
	 * that we can delegate to NS.
	 */
	if (sec_state == SECURE) {
		save_secure_context();
		ctx = restore_and_resume_ns_context();
	}

548
549
550
551
552
553
554
	/* Synchronously dispatch event */
	setup_ns_dispatch(map, se, ctx, &dispatch_jmp);
	begin_sdei_synchronous_dispatch(&dispatch_jmp);

	/*
	 * We reach here when client completes the event.
	 *
555
	 * If the cause of dispatch originally interrupted the Secure world,
556
557
558
559
560
561
	 * resume Secure.
	 *
	 * No need to save the Non-secure context ahead of a world switch: the
	 * Non-secure context was fully saved before dispatch, and has been
	 * returned to its pre-dispatch state.
	 */
562
	if (sec_state == SECURE)
563
		restore_and_resume_secure_context();
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
564
565

	/*
566
567
568
	 * The event was dispatched after receiving SDEI interrupt. With
	 * the event handling completed, EOI the corresponding
	 * interrupt.
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
569
	 */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
570
	if ((map->ev_num != SDEI_EVENT_0) && !is_map_bound(map)) {
571
572
573
574
575
		ERROR("Invalid SDEI mapping: ev=%u\n", map->ev_num);
		panic();
	}
	plat_ic_end_of_interrupt(intr_raw);

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
576
577
578
	return 0;
}

579
580
581
582
583
584
585
586
587
588
589
590
/*
 * Explicitly dispatch the given SDEI event.
 *
 * When calling this API, the caller must be prepared for the SDEI dispatcher to
 * restore and make Non-secure context as active. This call returns only after
 * the client has completed the dispatch. Then, the Non-secure context will be
 * active, and the following ERET will return to Non-secure.
 *
 * Should the caller require re-entry to Secure, it must restore the Secure
 * context and program registers for ERET.
 */
int sdei_dispatch_event(int ev_num)
591
592
593
{
	sdei_entry_t *se;
	sdei_ev_map_t *map;
594
	cpu_context_t *ns_ctx;
595
596
	sdei_dispatch_context_t *disp_ctx;
	sdei_cpu_state_t *state;
597
	jmp_buf dispatch_jmp;
598
599
600

	/* Can't dispatch if events are masked on this PE */
	state = sdei_get_this_pe_state();
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
601
	if (state->pe_masked)
602
603
604
605
606
607
608
609
		return -1;

	/* Event 0 can't be dispatched */
	if (ev_num == SDEI_EVENT_0)
		return -1;

	/* Locate mapping corresponding to this event */
	map = find_event_map(ev_num);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
610
	if (map == NULL)
611
612
		return -1;

613
614
	/* Only explicit events can be dispatched */
	if (!is_map_explicit(map))
615
616
617
618
		return -1;

	/* Examine state of dispatch stack */
	disp_ctx = get_outstanding_dispatch();
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
619
	if (disp_ctx != NULL) {
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
		/*
		 * There's an outstanding dispatch. If the outstanding dispatch
		 * is critical, no more dispatches are possible.
		 */
		if (is_event_critical(disp_ctx->map))
			return -1;

		/*
		 * If the outstanding dispatch is Normal, only critical events
		 * can be dispatched.
		 */
		if (is_event_normal(map))
			return -1;
	}

	se = get_event_entry(map);
	if (!can_sdei_state_trans(se, DO_DISPATCH))
		return -1;

	/*
640
641
	 * Prepare for NS dispatch by restoring the Non-secure context and
	 * marking that as active.
642
	 */
643
644
	ns_ctx = restore_and_resume_ns_context();

645
646
647
	/* Activate the priority corresponding to the event being dispatched */
	ehf_activate_priority(sdei_event_priority(map));

648
649
650
	/* Dispatch event synchronously */
	setup_ns_dispatch(map, se, ns_ctx, &dispatch_jmp);
	begin_sdei_synchronous_dispatch(&dispatch_jmp);
651
652

	/*
653
654
655
656
	 * We reach here when client completes the event.
	 *
	 * Deactivate the priority level that was activated at the time of
	 * explicit dispatch.
657
	 */
658
	ehf_deactivate_priority(sdei_event_priority(map));
659
660
661
662

	return 0;
}

663
static void end_sdei_synchronous_dispatch(jmp_buf *buffer)
664
{
665
	longjmp(*buffer, 1);
666
667
}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
668
int sdei_event_complete(bool resume, uint64_t pc)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
669
670
671
672
673
674
675
676
677
{
	sdei_dispatch_context_t *disp_ctx;
	sdei_entry_t *se;
	sdei_ev_map_t *map;
	cpu_context_t *ctx;
	sdei_action_t act;
	unsigned int client_el = sdei_client_el();

	/* Return error if called without an active event */
678
	disp_ctx = get_outstanding_dispatch();
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
679
	if (disp_ctx == NULL)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
680
681
682
683
684
685
686
		return SDEI_EDENY;

	/* Validate resumption point */
	if (resume && (plat_sdei_validate_entry_point(pc, client_el) != 0))
		return SDEI_EDENY;

	map = disp_ctx->map;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
687
	assert(map != NULL);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
688
689
	se = get_event_entry(map);

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
690
691
692
	if (is_event_shared(map))
		sdei_map_lock(map);

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
693
694
695
696
697
698
699
	act = resume ? DO_COMPLETE_RESUME : DO_COMPLETE;
	if (!can_sdei_state_trans(se, act)) {
		if (is_event_shared(map))
			sdei_map_unlock(map);
		return SDEI_EDENY;
	}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
700
701
702
	if (is_event_shared(map))
		sdei_map_unlock(map);

703
	/* Having done sanity checks, pop dispatch */
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
704
	(void) pop_dispatch();
705
706
707
708

	SDEI_LOG("EOI:%lx, %d spsr:%lx elr:%lx\n", read_mpidr_el1(),
			map->ev_num, read_spsr_el3(), read_elr_el3());

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
	/*
	 * Restore Non-secure to how it was originally interrupted. Once done,
	 * it's up-to-date with the saved copy.
	 */
	ctx = cm_get_context(NON_SECURE);
	restore_event_ctx(disp_ctx, ctx);

	if (resume) {
		/*
		 * Complete-and-resume call. Prepare the Non-secure context
		 * (currently active) for complete and resume.
		 */
		cm_set_elr_spsr_el3(NON_SECURE, pc, SPSR_64(client_el,
					MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS));

		/*
		 * Make it look as if a synchronous exception were taken at the
		 * supplied Non-secure resumption point. Populate SPSR and
		 * ELR_ELx so that an ERET from there works as expected.
		 *
		 * The assumption is that the client, if necessary, would have
		 * saved any live content in these registers before making this
		 * call.
		 */
		if (client_el == MODE_EL2) {
			write_elr_el2(disp_ctx->elr_el3);
			write_spsr_el2(disp_ctx->spsr_el3);
		} else {
			/* EL1 */
			write_elr_el1(disp_ctx->elr_el3);
			write_spsr_el1(disp_ctx->spsr_el3);
		}
	}

743
	/* End the outstanding dispatch */
744
	end_sdei_synchronous_dispatch(disp_ctx->dispatch_jmp);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
745
746
747
748

	return 0;
}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
749
int64_t sdei_event_context(void *handle, unsigned int param)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
750
751
752
753
754
755
756
757
{
	sdei_dispatch_context_t *disp_ctx;

	if (param >= SDEI_SAVED_GPREGS)
		return SDEI_EINVAL;

	/* Get outstanding dispatch on this CPU */
	disp_ctx = get_outstanding_dispatch();
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
758
	if (disp_ctx == NULL)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
759
760
		return SDEI_EDENY;

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
761
	assert(disp_ctx->map != NULL);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
762
763
764
765
766
767
768
769
770

	if (!can_sdei_state_trans(get_event_entry(disp_ctx->map), DO_CONTEXT))
		return SDEI_EDENY;

	/*
	 * No locking is required for the Running status as this is the only CPU
	 * which can complete the event
	 */

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
771
	return (int64_t) disp_ctx->x[param];
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
772
}