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 | |
| 9 | #include <types.h> |
| 10 | |
| 11 | /*********************************************************************** |
| 12 | * Utility functions to be used across different host platform variants. |
| 13 | **********************************************************************/ |
| 14 | |
| 15 | /* Maximum number of sysregs for which we can install callbacks */ |
| 16 | #define SYSREG_MAX_CBS (10U) |
| 17 | |
| 18 | /* Maximum size allowed for a sysreg name */ |
| 19 | #define MAX_SYSREG_NAME_LEN (25U) |
| 20 | |
| 21 | /* |
| 22 | * Callback prototype invoked when a sysreg is read. |
| 23 | * |
| 24 | * Arguments: |
| 25 | * reg - Pointer to the emulated register |
| 26 | * |
| 27 | * Returns: |
| 28 | * Value read from the emulated sysreg |
| 29 | */ |
| 30 | typedef u_register_t (*rd_cb_t)(u_register_t *reg); |
| 31 | |
| 32 | /* |
| 33 | * Callback prototype invoked when a sysreg is written. |
| 34 | * |
| 35 | * Arguments: |
| 36 | * val - Value to be written to the sysreg |
| 37 | * reg - Pointer to the emulated sysreg |
| 38 | * |
| 39 | * Returns: |
| 40 | * Void |
| 41 | */ |
| 42 | typedef void (*wr_cb_t)(u_register_t val, u_register_t *reg); |
| 43 | |
| 44 | /* |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 45 | * Structure to hold the callback pointers for register access emulation. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 46 | */ |
| 47 | struct sysreg_cb { |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 48 | rd_cb_t rd_cb; |
| 49 | wr_cb_t wr_cb; |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 50 | /* |
| 51 | * Pointer to the instance of the register corresponding to the |
| 52 | * current CPU |
| 53 | */ |
| 54 | u_register_t *reg; |
| 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * Structure to hold register access emulation data. |
| 59 | */ |
| 60 | struct sysreg_data { |
| 61 | char name[MAX_SYSREG_NAME_LEN + 1U]; |
| 62 | struct sysreg_cb callbacks; |
| 63 | u_register_t value[MAX_CPUS]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * Return the callbacks for a given sysreg or NULL |
| 68 | * if no callbacks are found. |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 69 | * |
| 70 | * Arguments: |
| 71 | * name - String containing the name of the sysreg. The name cannot exceed |
| 72 | * MAX_SYSREG_NAME_LEN (excluding the terminatig NULL character) |
| 73 | * or it will be truncated. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 74 | */ |
| 75 | struct sysreg_cb *host_util_get_sysreg_cb(char *name); |
| 76 | |
| 77 | /* |
| 78 | * Setup callbacks for sysreg read and write operations. |
| 79 | * |
| 80 | * This API allows to setup callbacks for each sysreg to be called upon |
| 81 | * read or write operations. This allows to control what to return on |
| 82 | * a read or how to process a write. |
| 83 | * |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 84 | * Arguments: |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 85 | * name - String containing the name of the sysreg. The name of |
| 86 | * the sysreg cannot exceed MAX_SYSREG_NAME_LEN (excluding |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 87 | * the terminating NULL character) or it will be truncated. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 88 | * rd_cb - Callback to be invoked on a read operation. |
| 89 | * wr_cb - Callback to be invoked on a write operation. |
| 90 | * init - Value used as reset value for the sysreg. |
| 91 | * |
| 92 | * Returns: |
| 93 | * 0 on success or a negative error code otherwise. |
| 94 | */ |
| 95 | int host_util_set_sysreg_cb(char *name, rd_cb_t rd_cb, wr_cb_t wr_cb, |
| 96 | u_register_t init); |
| 97 | |
| 98 | /* |
| 99 | * Setup generic callbacks for sysreg read and write operations. |
| 100 | * |
| 101 | * This API allows to setup generic callbacks for each sysreg to be called upon |
| 102 | * read or write operations. |
| 103 | * |
| 104 | * Arguments: |
| 105 | * name - String containing the name of the sysreg. The name of |
| 106 | * the sysreg cannot exceed MAX_SYSREG_NAME_LEN (excluding |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 107 | * the terminating NULL character) or it will be truncated. |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 108 | * init - Value used as reset value for the sysreg. |
| 109 | * |
| 110 | * Returns: |
| 111 | * 0 on success or a negative error code otherwise. |
| 112 | */ |
| 113 | int host_util_set_default_sysreg_cb(char *name, u_register_t init); |
| 114 | |
| 115 | /* |
| 116 | * Clear the list of sysreg callbacks. |
| 117 | */ |
| 118 | void host_util_reset_all_sysreg_cb(void); |
| 119 | |
| 120 | /* |
| 121 | * Return the configured address for the granule base. |
| 122 | */ |
| 123 | unsigned long host_util_get_granule_base(void); |
| 124 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 125 | /* |
| 126 | * Set the current CPU emulated by the platform. |
| 127 | */ |
| 128 | void host_util_set_cpuid(unsigned int cpuid); |
| 129 | |
Mate Toth-Pal | aead06f | 2023-03-02 10:17:09 +0100 | [diff] [blame^] | 130 | /* |
| 131 | * Return the address of the EL3 RMM shared buffer. |
| 132 | */ |
| 133 | unsigned char *host_util_get_el3_rmm_shared_buffer(void); |
| 134 | |
| 135 | /* |
| 136 | * Performs some initialization needed before RMM can be run, such as |
| 137 | * setting up callbacks for sysreg access. |
| 138 | */ |
| 139 | void host_util_setup_sysreg_and_boot_manifest(void); |
| 140 | |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 141 | #endif /* HOST_UTILS_H */ |