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 | /* |
| 45 | * Structure to hold the callback pointers and value of the emulated sysreg. |
| 46 | */ |
| 47 | struct sysreg_cb { |
| 48 | char sysreg[MAX_SYSREG_NAME_LEN + 1U]; |
| 49 | rd_cb_t rd_cb; |
| 50 | wr_cb_t wr_cb; |
| 51 | u_register_t value; |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * Return the callbacks for a given sysreg or NULL |
| 56 | * if no callbacks are found. |
| 57 | */ |
| 58 | struct sysreg_cb *host_util_get_sysreg_cb(char *name); |
| 59 | |
| 60 | /* |
| 61 | * Setup callbacks for sysreg read and write operations. |
| 62 | * |
| 63 | * This API allows to setup callbacks for each sysreg to be called upon |
| 64 | * read or write operations. This allows to control what to return on |
| 65 | * a read or how to process a write. |
| 66 | * |
| 67 | * Argsuments: |
| 68 | * name - String containing the name of the sysreg. The name of |
| 69 | * the sysreg cannot exceed MAX_SYSREG_NAME_LEN (excluding |
| 70 | * the terminating null character) or it will be truncated. |
| 71 | * rd_cb - Callback to be invoked on a read operation. |
| 72 | * wr_cb - Callback to be invoked on a write operation. |
| 73 | * init - Value used as reset value for the sysreg. |
| 74 | * |
| 75 | * Returns: |
| 76 | * 0 on success or a negative error code otherwise. |
| 77 | */ |
| 78 | int host_util_set_sysreg_cb(char *name, rd_cb_t rd_cb, wr_cb_t wr_cb, |
| 79 | u_register_t init); |
| 80 | |
| 81 | /* |
| 82 | * Setup generic callbacks for sysreg read and write operations. |
| 83 | * |
| 84 | * This API allows to setup generic callbacks for each sysreg to be called upon |
| 85 | * read or write operations. |
| 86 | * |
| 87 | * Arguments: |
| 88 | * name - String containing the name of the sysreg. The name of |
| 89 | * the sysreg cannot exceed MAX_SYSREG_NAME_LEN (excluding |
| 90 | * the terminating null character) or it will be truncated. |
| 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_default_sysreg_cb(char *name, u_register_t init); |
| 97 | |
| 98 | /* |
| 99 | * Clear the list of sysreg callbacks. |
| 100 | */ |
| 101 | void host_util_reset_all_sysreg_cb(void); |
| 102 | |
| 103 | /* |
| 104 | * Return the configured address for the granule base. |
| 105 | */ |
| 106 | unsigned long host_util_get_granule_base(void); |
| 107 | |
| 108 | #endif /* HOST_UTILS_H */ |