Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
| 6 | #ifndef HOST_UTILS_H |
| 7 | #define HOST_UTILS_H |
| 8 | |
Javier Almansa Sobrino | 48d68a7 | 2023-03-02 12:26:39 +0000 | [diff] [blame] | 9 | #include <stddef.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 10 | #include <types.h> |
| 11 | |
| 12 | /*********************************************************************** |
| 13 | * Utility functions to be used across different host platform variants. |
| 14 | **********************************************************************/ |
| 15 | |
| 16 | /* Maximum number of sysregs for which we can install callbacks */ |
Soby Mathew | a694cca | 2023-04-24 03:42:45 +0100 | [diff] [blame] | 17 | #define SYSREG_MAX_CBS (15U) |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 18 | |
| 19 | /* Maximum size allowed for a sysreg name */ |
| 20 | #define MAX_SYSREG_NAME_LEN (25U) |
| 21 | |
| 22 | /* |
| 23 | * Callback prototype invoked when a sysreg is read. |
| 24 | * |
| 25 | * Arguments: |
| 26 | * reg - Pointer to the emulated register |
| 27 | * |
| 28 | * Returns: |
| 29 | * Value read from the emulated sysreg |
| 30 | */ |
| 31 | typedef u_register_t (*rd_cb_t)(u_register_t *reg); |
| 32 | |
| 33 | /* |
| 34 | * Callback prototype invoked when a sysreg is written. |
| 35 | * |
| 36 | * Arguments: |
| 37 | * val - Value to be written to the sysreg |
| 38 | * reg - Pointer to the emulated sysreg |
| 39 | * |
| 40 | * Returns: |
| 41 | * Void |
| 42 | */ |
| 43 | typedef void (*wr_cb_t)(u_register_t val, u_register_t *reg); |
| 44 | |
| 45 | /* |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 46 | * Structure to hold the callback pointers for register access emulation. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 47 | */ |
| 48 | struct sysreg_cb { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 49 | rd_cb_t rd_cb; |
| 50 | wr_cb_t wr_cb; |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 51 | /* |
| 52 | * Pointer to the instance of the register corresponding to the |
| 53 | * current CPU |
| 54 | */ |
| 55 | u_register_t *reg; |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * Structure to hold register access emulation data. |
| 60 | */ |
| 61 | struct sysreg_data { |
| 62 | char name[MAX_SYSREG_NAME_LEN + 1U]; |
| 63 | struct sysreg_cb callbacks; |
| 64 | u_register_t value[MAX_CPUS]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * Return the callbacks for a given sysreg or NULL |
| 69 | * if no callbacks are found. |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 70 | * |
| 71 | * Arguments: |
| 72 | * name - String containing the name of the sysreg. The name cannot exceed |
| 73 | * MAX_SYSREG_NAME_LEN (excluding the terminatig NULL character) |
| 74 | * or it will be truncated. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 75 | */ |
| 76 | struct sysreg_cb *host_util_get_sysreg_cb(char *name); |
| 77 | |
| 78 | /* |
| 79 | * Setup callbacks for sysreg read and write operations. |
| 80 | * |
| 81 | * This API allows to setup callbacks for each sysreg to be called upon |
| 82 | * read or write operations. This allows to control what to return on |
| 83 | * a read or how to process a write. |
| 84 | * |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 85 | * Arguments: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 86 | * name - String containing the name of the sysreg. The name of |
| 87 | * the sysreg cannot exceed MAX_SYSREG_NAME_LEN (excluding |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 88 | * the terminating NULL character) or it will be truncated. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 89 | * rd_cb - Callback to be invoked on a read operation. |
| 90 | * wr_cb - Callback to be invoked on a write operation. |
| 91 | * init - Value used as reset value for the sysreg. |
| 92 | * |
| 93 | * Returns: |
| 94 | * 0 on success or a negative error code otherwise. |
| 95 | */ |
| 96 | int host_util_set_sysreg_cb(char *name, rd_cb_t rd_cb, wr_cb_t wr_cb, |
| 97 | u_register_t init); |
| 98 | |
| 99 | /* |
| 100 | * Setup generic callbacks for sysreg read and write operations. |
| 101 | * |
| 102 | * This API allows to setup generic callbacks for each sysreg to be called upon |
| 103 | * read or write operations. |
| 104 | * |
| 105 | * Arguments: |
| 106 | * name - String containing the name of the sysreg. The name of |
| 107 | * the sysreg cannot exceed MAX_SYSREG_NAME_LEN (excluding |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 108 | * the terminating NULL character) or it will be truncated. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 109 | * init - Value used as reset value for the sysreg. |
| 110 | * |
| 111 | * Returns: |
| 112 | * 0 on success or a negative error code otherwise. |
| 113 | */ |
| 114 | int host_util_set_default_sysreg_cb(char *name, u_register_t init); |
| 115 | |
| 116 | /* |
Javier Almansa Sobrino | 48d68a7 | 2023-03-02 12:26:39 +0000 | [diff] [blame] | 117 | * Save the sysreg state across all PEs in the system along with registered |
| 118 | * callbacks. This function must only be used during RMM runtime bring-up, |
| 119 | * at a point wherein the system is initialized properly and can restored |
| 120 | * for later test iterations. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 121 | */ |
Javier Almansa Sobrino | 48d68a7 | 2023-03-02 12:26:39 +0000 | [diff] [blame] | 122 | void host_util_take_sysreg_snapshot(void); |
| 123 | |
| 124 | /* |
| 125 | * Restore a saved sysreg state and associated callbacks. The state is already |
| 126 | * assumed to be saved prior to calling this API. |
| 127 | */ |
| 128 | void host_util_restore_sysreg_snapshot(void); |
| 129 | |
| 130 | /* |
| 131 | * Zero all sysreg values and unregister all sysreg callbacks. |
| 132 | */ |
| 133 | void host_util_zero_sysregs_and_cbs(void); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Return the configured address for the granule base. |
| 137 | */ |
| 138 | unsigned long host_util_get_granule_base(void); |
| 139 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 140 | /* |
| 141 | * Set the current CPU emulated by the platform. |
| 142 | */ |
| 143 | void host_util_set_cpuid(unsigned int cpuid); |
| 144 | |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame] | 145 | /* |
| 146 | * Return the address of the EL3 RMM shared buffer. |
| 147 | */ |
| 148 | unsigned char *host_util_get_el3_rmm_shared_buffer(void); |
| 149 | |
| 150 | /* |
| 151 | * Performs some initialization needed before RMM can be run, such as |
| 152 | * setting up callbacks for sysreg access. |
| 153 | */ |
| 154 | void host_util_setup_sysreg_and_boot_manifest(void); |
| 155 | |
Soby Mathew | a694cca | 2023-04-24 03:42:45 +0100 | [diff] [blame] | 156 | /* |
| 157 | * Runs the realm entrypoint as programmed in elr_el2 and resets |
| 158 | * the esr_el2 before entering the Realm. |
| 159 | */ |
| 160 | int host_util_rec_run(unsigned long *regs); |
| 161 | |
| 162 | /* Prototype for Realm entrypoint */ |
| 163 | typedef int (*realm_entrypoint_t)(unsigned long *); |
| 164 | |
| 165 | /* Helper for invoking RSI calls */ |
| 166 | int host_util_rsi_helper(realm_entrypoint_t ep); |
| 167 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 168 | #endif /* HOST_UTILS_H */ |