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 | |
Javier Almansa Sobrino | ed93259 | 2023-01-24 12:50:41 +0000 | [diff] [blame^] | 6 | #include <assert.h> |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 7 | #include <debug.h> |
| 8 | #include <errno.h> |
| 9 | #include <host_defs.h> |
| 10 | #include <host_utils.h> |
| 11 | #include <plat_common.h> |
| 12 | #include <string.h> |
| 13 | #include <xlat_tables.h> |
| 14 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 15 | static struct sysreg_data sysregs[SYSREG_MAX_CBS]; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 16 | static unsigned int installed_cb_idx; |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 17 | static unsigned int current_cpuid; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Allocate memory to emulate physical memory to initialize the |
| 21 | * granule library. |
| 22 | */ |
| 23 | static unsigned char granules_buffer[HOST_MEM_SIZE] __aligned(GRANULE_SIZE); |
| 24 | |
| 25 | /* |
| 26 | * Generic callback to access a sysreg for reading. |
| 27 | */ |
| 28 | static u_register_t sysreg_rd_cb(u_register_t *reg) |
| 29 | { |
| 30 | return *reg; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Generic callback to access a sysreg for writing. |
| 35 | */ |
| 36 | static void sysreg_wr_cb(u_register_t val, u_register_t *reg) |
| 37 | { |
| 38 | *reg = val; |
| 39 | } |
| 40 | |
| 41 | struct sysreg_cb *host_util_get_sysreg_cb(char *name) |
| 42 | { |
| 43 | for (unsigned int i = 0U; i < SYSREG_MAX_CBS; i++) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 44 | if (strncmp(name, &sysregs[i].name[0], |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 45 | MAX_SYSREG_NAME_LEN) == 0) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Get a pointer to the register value for the |
| 49 | * current CPU. |
| 50 | */ |
| 51 | sysregs[i].callbacks.reg = |
| 52 | &(sysregs[i].value[current_cpuid]); |
| 53 | return &sysregs[i].callbacks; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | return (struct sysreg_cb *)NULL; |
| 58 | } |
| 59 | |
| 60 | int host_util_set_sysreg_cb(char *name, rd_cb_t rd_cb, wr_cb_t wr_cb, |
| 61 | u_register_t init) |
| 62 | { |
| 63 | if (installed_cb_idx < SYSREG_MAX_CBS) { |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 64 | sysregs[installed_cb_idx].callbacks.rd_cb = rd_cb; |
| 65 | sysregs[installed_cb_idx].callbacks.wr_cb = wr_cb; |
| 66 | sysregs[installed_cb_idx].callbacks.reg = |
| 67 | (u_register_t *)NULL; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 68 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 69 | for (unsigned int i = 0U; i < MAX_CPUS; i++) { |
| 70 | sysregs[installed_cb_idx].value[i] = init; |
| 71 | } |
| 72 | |
| 73 | (void)strncpy(&(sysregs[installed_cb_idx].name[0]), |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 74 | &name[0], MAX_SYSREG_NAME_LEN); |
| 75 | |
| 76 | /* |
| 77 | * Add a string termination character in case the |
| 78 | * name were truncated. |
| 79 | */ |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 80 | sysregs[installed_cb_idx].name[MAX_SYSREG_NAME_LEN] = '\0'; |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 81 | |
| 82 | ++installed_cb_idx; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | return -ENOMEM; |
| 88 | } |
| 89 | |
| 90 | void host_util_reset_all_sysreg_cb(void) |
| 91 | { |
| 92 | |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 93 | (void)memset((void *)sysregs, 0, |
| 94 | sizeof(struct sysreg_data) * SYSREG_MAX_CBS); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 95 | |
| 96 | installed_cb_idx = 0U; |
| 97 | } |
| 98 | |
| 99 | int host_util_set_default_sysreg_cb(char *name, u_register_t init) |
| 100 | { |
| 101 | return host_util_set_sysreg_cb(name, &sysreg_rd_cb, |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 102 | &sysreg_wr_cb, init); |
Soby Mathew | b4c6df4 | 2022-11-09 11:13:29 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | unsigned long host_util_get_granule_base(void) |
| 106 | { |
| 107 | return (unsigned long)granules_buffer; |
| 108 | } |
Javier Almansa Sobrino | 9929a79 | 2022-11-22 16:11:13 +0000 | [diff] [blame] | 109 | |
| 110 | void host_util_set_cpuid(unsigned int cpuid) |
| 111 | { |
| 112 | assert(cpuid < MAX_CPUS); |
| 113 | |
| 114 | current_cpuid = cpuid; |
| 115 | } |