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 | #include <debug.h> |
| 7 | #include <errno.h> |
| 8 | #include <host_defs.h> |
| 9 | #include <host_utils.h> |
| 10 | #include <plat_common.h> |
| 11 | #include <string.h> |
| 12 | #include <xlat_tables.h> |
| 13 | |
| 14 | static struct sysreg_cb callbacks[SYSREG_MAX_CBS]; |
| 15 | static unsigned int installed_cb_idx; |
| 16 | |
| 17 | /* |
| 18 | * Allocate memory to emulate physical memory to initialize the |
| 19 | * granule library. |
| 20 | */ |
| 21 | static unsigned char granules_buffer[HOST_MEM_SIZE] __aligned(GRANULE_SIZE); |
| 22 | |
| 23 | /* |
| 24 | * Generic callback to access a sysreg for reading. |
| 25 | */ |
| 26 | static u_register_t sysreg_rd_cb(u_register_t *reg) |
| 27 | { |
| 28 | return *reg; |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * Generic callback to access a sysreg for writing. |
| 33 | */ |
| 34 | static void sysreg_wr_cb(u_register_t val, u_register_t *reg) |
| 35 | { |
| 36 | *reg = val; |
| 37 | } |
| 38 | |
| 39 | struct sysreg_cb *host_util_get_sysreg_cb(char *name) |
| 40 | { |
| 41 | for (unsigned int i = 0U; i < SYSREG_MAX_CBS; i++) { |
| 42 | if (strncmp(name, &callbacks[i].sysreg[0], |
| 43 | MAX_SYSREG_NAME_LEN) == 0) { |
| 44 | return &callbacks[i]; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return (struct sysreg_cb *)NULL; |
| 49 | } |
| 50 | |
| 51 | int host_util_set_sysreg_cb(char *name, rd_cb_t rd_cb, wr_cb_t wr_cb, |
| 52 | u_register_t init) |
| 53 | { |
| 54 | if (installed_cb_idx < SYSREG_MAX_CBS) { |
| 55 | callbacks[installed_cb_idx].rd_cb = rd_cb; |
| 56 | callbacks[installed_cb_idx].wr_cb = wr_cb; |
| 57 | callbacks[installed_cb_idx].value = init; |
| 58 | |
| 59 | (void)strncpy(&(callbacks[installed_cb_idx].sysreg[0]), |
| 60 | &name[0], MAX_SYSREG_NAME_LEN); |
| 61 | |
| 62 | /* |
| 63 | * Add a string termination character in case the |
| 64 | * name were truncated. |
| 65 | */ |
| 66 | callbacks[installed_cb_idx].sysreg[MAX_SYSREG_NAME_LEN] = '\0'; |
| 67 | |
| 68 | ++installed_cb_idx; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | return -ENOMEM; |
| 74 | } |
| 75 | |
| 76 | void host_util_reset_all_sysreg_cb(void) |
| 77 | { |
| 78 | |
| 79 | (void)memset((void *)callbacks, 0, |
| 80 | sizeof(struct sysreg_cb) * SYSREG_MAX_CBS); |
| 81 | |
| 82 | installed_cb_idx = 0U; |
| 83 | } |
| 84 | |
| 85 | int host_util_set_default_sysreg_cb(char *name, u_register_t init) |
| 86 | { |
| 87 | return host_util_set_sysreg_cb(name, &sysreg_rd_cb, |
| 88 | &sysreg_wr_cb, init); |
| 89 | } |
| 90 | |
| 91 | unsigned long host_util_get_granule_base(void) |
| 92 | { |
| 93 | return (unsigned long)granules_buffer; |
| 94 | } |