Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 7 | #ifndef SPM_PRIVATE_H |
| 8 | #define SPM_PRIVATE_H |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 9 | |
| 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 Diaz | fb76396 | 2018-07-03 16:54:33 +0100 | [diff] [blame] | 32 | /* Value returned by spm_sp_synchronous_entry() when a partition is preempted */ |
| 33 | #define SPM_SECURE_PARTITION_PREEMPTED U(0x1234) |
| 34 | |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 35 | #ifndef __ASSEMBLY__ |
| 36 | |
| 37 | #include <stdint.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 38 | |
| 39 | #include <lib/xlat_tables/xlat_tables_v2.h> |
| 40 | #include <lib/spinlock.h> |
| 41 | #include <services/sp_res_desc.h> |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 42 | |
Antonio Nino Diaz | da50cd0 | 2018-06-15 16:21:01 +0100 | [diff] [blame] | 43 | typedef enum sp_state { |
Antonio Nino Diaz | c4f2752 | 2018-05-23 09:09:41 +0100 | [diff] [blame] | 44 | SP_STATE_RESET = 0, |
| 45 | SP_STATE_IDLE, |
| 46 | SP_STATE_BUSY |
| 47 | } sp_state_t; |
| 48 | |
Antonio Nino Diaz | 2875931 | 2018-05-22 16:26:48 +0100 | [diff] [blame] | 49 | typedef struct sp_context { |
Antonio Nino Diaz | 8cc23f9 | 2018-10-30 11:35:30 +0000 | [diff] [blame] | 50 | /* 1 if the partition is present, 0 otherwise */ |
| 51 | int is_present; |
| 52 | |
Antonio Nino Diaz | 840627f | 2018-11-27 08:36:02 +0000 | [diff] [blame] | 53 | /* Location of the image in physical memory */ |
| 54 | unsigned long long image_base; |
| 55 | size_t image_size; |
| 56 | |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 57 | uint64_t c_rt_ctx; |
| 58 | cpu_context_t cpu_ctx; |
Antonio Nino Diaz | 840627f | 2018-11-27 08:36:02 +0000 | [diff] [blame] | 59 | struct sp_res_desc rd; |
Antonio Nino Diaz | c4f2752 | 2018-05-23 09:09:41 +0100 | [diff] [blame] | 60 | |
Antonio Nino Diaz | 124a1fc | 2018-11-30 10:52:09 +0000 | [diff] [blame] | 61 | /* Translation tables context */ |
| 62 | xlat_ctx_t *xlat_ctx_handle; |
| 63 | spinlock_t xlat_ctx_lock; |
| 64 | |
Antonio Nino Diaz | c4f2752 | 2018-05-23 09:09:41 +0100 | [diff] [blame] | 65 | sp_state_t state; |
| 66 | spinlock_t state_lock; |
Antonio Nino Diaz | bb7d1cd | 2018-10-30 11:34:23 +0000 | [diff] [blame] | 67 | |
Antonio Nino Diaz | 44ef4eb | 2018-07-03 19:54:59 +0100 | [diff] [blame] | 68 | unsigned int request_count; |
| 69 | spinlock_t request_count_lock; |
| 70 | |
Antonio Nino Diaz | bb7d1cd | 2018-10-30 11:34:23 +0000 | [diff] [blame] | 71 | /* Base and size of the shared SPM<->SP buffer */ |
| 72 | uintptr_t spm_sp_buffer_base; |
| 73 | size_t spm_sp_buffer_size; |
Antonio Nino Diaz | 8c83ad8 | 2018-11-08 14:21:19 +0000 | [diff] [blame] | 74 | spinlock_t spm_sp_buffer_lock; |
Antonio Nino Diaz | 2875931 | 2018-05-22 16:26:48 +0100 | [diff] [blame] | 75 | } sp_context_t; |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 76 | |
Antonio Nino Diaz | 8c83ad8 | 2018-11-08 14:21:19 +0000 | [diff] [blame] | 77 | /* Functions used to enter/exit a Secure Partition synchronously */ |
Antonio Nino Diaz | fb76396 | 2018-07-03 16:54:33 +0100 | [diff] [blame] | 78 | uint64_t spm_sp_synchronous_entry(sp_context_t *sp_ctx, int can_preempt); |
Antonio Nino Diaz | 8c83ad8 | 2018-11-08 14:21:19 +0000 | [diff] [blame] | 79 | __dead2 void spm_sp_synchronous_exit(uint64_t rc); |
| 80 | |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 81 | /* Assembly helpers */ |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 82 | uint64_t spm_secure_partition_enter(uint64_t *c_rt_ctx); |
| 83 | void __dead2 spm_secure_partition_exit(uint64_t c_rt_ctx, uint64_t ret); |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 84 | |
Antonio Nino Diaz | bb7d1cd | 2018-10-30 11:34:23 +0000 | [diff] [blame] | 85 | /* Secure Partition setup */ |
Antonio Nino Diaz | 2875931 | 2018-05-22 16:26:48 +0100 | [diff] [blame] | 86 | void spm_sp_setup(sp_context_t *sp_ctx); |
Antonio Nino Diaz | 2ac9a44 | 2018-05-23 11:40:46 +0100 | [diff] [blame] | 87 | |
Antonio Nino Diaz | 8c83ad8 | 2018-11-08 14:21:19 +0000 | [diff] [blame] | 88 | /* Secure Partition state management helpers */ |
| 89 | void sp_state_set(sp_context_t *sp_ptr, sp_state_t state); |
| 90 | void sp_state_wait_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to); |
| 91 | int sp_state_try_switch(sp_context_t *sp_ptr, sp_state_t from, sp_state_t to); |
| 92 | |
Antonio Nino Diaz | 44ef4eb | 2018-07-03 19:54:59 +0100 | [diff] [blame] | 93 | /* Functions to keep track of the number of active requests per SP */ |
| 94 | void spm_sp_request_increase(sp_context_t *sp_ctx); |
| 95 | void spm_sp_request_decrease(sp_context_t *sp_ctx); |
| 96 | int spm_sp_request_increase_if_zero(sp_context_t *sp_ctx); |
| 97 | |
Antonio Nino Diaz | bb7d1cd | 2018-10-30 11:34:23 +0000 | [diff] [blame] | 98 | /* Functions related to the translation tables management */ |
Antonio Nino Diaz | 8cc23f9 | 2018-10-30 11:35:30 +0000 | [diff] [blame] | 99 | xlat_ctx_t *spm_sp_xlat_context_alloc(void); |
Antonio Nino Diaz | bb7d1cd | 2018-10-30 11:34:23 +0000 | [diff] [blame] | 100 | void sp_map_memory_regions(sp_context_t *sp_ctx); |
Antonio Nino Diaz | 7b28b54 | 2018-05-22 16:45:35 +0100 | [diff] [blame] | 101 | |
Antonio Nino Diaz | 8cc23f9 | 2018-10-30 11:35:30 +0000 | [diff] [blame] | 102 | /* Functions to handle Secure Partition contexts */ |
| 103 | void spm_cpu_set_sp_ctx(unsigned int linear_id, sp_context_t *sp_ctx); |
| 104 | sp_context_t *spm_cpu_get_sp_ctx(unsigned int linear_id); |
Antonio Nino Diaz | b5b585a | 2018-11-08 14:20:07 +0000 | [diff] [blame] | 105 | sp_context_t *spm_sp_get_by_uuid(const uint32_t (*svc_uuid)[4]); |
Antonio Nino Diaz | 8cc23f9 | 2018-10-30 11:35:30 +0000 | [diff] [blame] | 106 | |
Antonio Nino Diaz | 8903641 | 2018-10-18 14:54:57 +0100 | [diff] [blame] | 107 | /* Functions to manipulate response and requests buffers */ |
| 108 | int spm_response_add(uint16_t client_id, uint16_t handle, uint32_t token, |
| 109 | u_register_t x1, u_register_t x2, u_register_t x3); |
| 110 | int spm_response_get(uint16_t client_id, uint16_t handle, uint32_t token, |
| 111 | u_register_t *x1, u_register_t *x2, u_register_t *x3); |
| 112 | |
Antonio Nino Diaz | c41f206 | 2017-10-24 10:07:35 +0100 | [diff] [blame] | 113 | #endif /* __ASSEMBLY__ */ |
| 114 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 115 | #endif /* SPM_PRIVATE_H */ |