blob: c1aad933a0c7d621438e8049f63e84932bf58daf [file] [log] [blame]
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01001/*
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +01002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef SPM_PRIVATE_H
8#define SPM_PRIVATE_H
Antonio Nino Diazc41f2062017-10-24 10:07:35 +01009
10#include <context.h>
11
12/*******************************************************************************
13 * Constants that allow assembler code to preserve callee-saved registers of the
14 * C runtime context while performing a security state switch.
15 ******************************************************************************/
16#define SP_C_RT_CTX_X19 0x0
17#define SP_C_RT_CTX_X20 0x8
18#define SP_C_RT_CTX_X21 0x10
19#define SP_C_RT_CTX_X22 0x18
20#define SP_C_RT_CTX_X23 0x20
21#define SP_C_RT_CTX_X24 0x28
22#define SP_C_RT_CTX_X25 0x30
23#define SP_C_RT_CTX_X26 0x38
24#define SP_C_RT_CTX_X27 0x40
25#define SP_C_RT_CTX_X28 0x48
26#define SP_C_RT_CTX_X29 0x50
27#define SP_C_RT_CTX_X30 0x58
28
29#define SP_C_RT_CTX_SIZE 0x60
30#define SP_C_RT_CTX_ENTRIES (SP_C_RT_CTX_SIZE >> DWORD_SHIFT)
31
Antonio Nino Diazfb763962018-07-03 16:54:33 +010032/* Value returned by spm_sp_synchronous_entry() when a partition is preempted */
33#define SPM_SECURE_PARTITION_PREEMPTED U(0x1234)
34
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010035#ifndef __ASSEMBLY__
36
Antonio Nino Diaz496f54d2018-01-08 09:59:33 +000037#include <spinlock.h>
Antonio Nino Diaz840627f2018-11-27 08:36:02 +000038#include <sp_res_desc.h>
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010039#include <stdint.h>
40#include <xlat_tables_v2.h>
41
Antonio Nino Diazda50cd02018-06-15 16:21:01 +010042typedef enum sp_state {
Antonio Nino Diazc4f27522018-05-23 09:09:41 +010043 SP_STATE_RESET = 0,
44 SP_STATE_IDLE,
45 SP_STATE_BUSY
46} sp_state_t;
47
Antonio Nino Diaz28759312018-05-22 16:26:48 +010048typedef struct sp_context {
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +000049 /* 1 if the partition is present, 0 otherwise */
50 int is_present;
51
Antonio Nino Diaz840627f2018-11-27 08:36:02 +000052 /* Location of the image in physical memory */
53 unsigned long long image_base;
54 size_t image_size;
55
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010056 uint64_t c_rt_ctx;
57 cpu_context_t cpu_ctx;
Antonio Nino Diaz840627f2018-11-27 08:36:02 +000058 struct sp_res_desc rd;
Antonio Nino Diazc4f27522018-05-23 09:09:41 +010059
Antonio Nino Diaz124a1fc2018-11-30 10:52:09 +000060 /* Translation tables context */
61 xlat_ctx_t *xlat_ctx_handle;
62 spinlock_t xlat_ctx_lock;
63
Antonio Nino Diazc4f27522018-05-23 09:09:41 +010064 sp_state_t state;
65 spinlock_t state_lock;
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000066
Antonio Nino Diaz44ef4eb2018-07-03 19:54:59 +010067 unsigned int request_count;
68 spinlock_t request_count_lock;
69
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000070 /* Base and size of the shared SPM<->SP buffer */
71 uintptr_t spm_sp_buffer_base;
72 size_t spm_sp_buffer_size;
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +000073 spinlock_t spm_sp_buffer_lock;
Antonio Nino Diaz28759312018-05-22 16:26:48 +010074} sp_context_t;
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010075
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +000076/* Functions used to enter/exit a Secure Partition synchronously */
Antonio Nino Diazfb763962018-07-03 16:54:33 +010077uint64_t spm_sp_synchronous_entry(sp_context_t *sp_ctx, int can_preempt);
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +000078__dead2 void spm_sp_synchronous_exit(uint64_t rc);
79
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010080/* Assembly helpers */
Antonio Nino Diazc41f2062017-10-24 10:07:35 +010081uint64_t spm_secure_partition_enter(uint64_t *c_rt_ctx);
82void __dead2 spm_secure_partition_exit(uint64_t c_rt_ctx, uint64_t ret);
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010083
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000084/* Secure Partition setup */
Antonio Nino Diaz28759312018-05-22 16:26:48 +010085void spm_sp_setup(sp_context_t *sp_ctx);
Antonio Nino Diaz2ac9a442018-05-23 11:40:46 +010086
Antonio Nino Diaz8c83ad82018-11-08 14:21:19 +000087/* Secure Partition state management helpers */
88void sp_state_set(sp_context_t *sp_ptr, sp_state_t state);
89void sp_state_wait_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to);
90int sp_state_try_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to);
91
Antonio Nino Diaz44ef4eb2018-07-03 19:54:59 +010092/* Functions to keep track of the number of active requests per SP */
93void spm_sp_request_increase(sp_context_t *sp_ctx);
94void spm_sp_request_decrease(sp_context_t *sp_ctx);
95int spm_sp_request_increase_if_zero(sp_context_t *sp_ctx);
96
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000097/* Functions related to the translation tables management */
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +000098xlat_ctx_t *spm_sp_xlat_context_alloc(void);
Antonio Nino Diazbb7d1cd2018-10-30 11:34:23 +000099void sp_map_memory_regions(sp_context_t *sp_ctx);
Antonio Nino Diaz7b28b542018-05-22 16:45:35 +0100100
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000101/* Functions to handle Secure Partition contexts */
102void spm_cpu_set_sp_ctx(unsigned int linear_id, sp_context_t *sp_ctx);
103sp_context_t *spm_cpu_get_sp_ctx(unsigned int linear_id);
Antonio Nino Diazb5b585a2018-11-08 14:20:07 +0000104sp_context_t *spm_sp_get_by_uuid(const uint32_t (*svc_uuid)[4]);
Antonio Nino Diaz8cc23f92018-10-30 11:35:30 +0000105
Antonio Nino Diaz89036412018-10-18 14:54:57 +0100106/* Functions to manipulate response and requests buffers */
107int spm_response_add(uint16_t client_id, uint16_t handle, uint32_t token,
108 u_register_t x1, u_register_t x2, u_register_t x3);
109int spm_response_get(uint16_t client_id, uint16_t handle, uint32_t token,
110 u_register_t *x1, u_register_t *x2, u_register_t *x3);
111
Antonio Nino Diazc41f2062017-10-24 10:07:35 +0100112#endif /* __ASSEMBLY__ */
113
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000114#endif /* SPM_PRIVATE_H */