blob: c90ba483aba91505fc3fc8dcb6855ee9b5602bf6 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001/*
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 Sobrino48d68a72023-03-02 12:26:39 +00009#include <stddef.h>
Soby Mathewb4c6df42022-11-09 11:13:29 +000010#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 */
Chuyue Luofa8973c2024-03-27 20:39:22 +000017#define SYSREG_MAX_CBS (30U)
Soby Mathewb4c6df42022-11-09 11:13:29 +000018
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 */
31typedef 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 */
43typedef void (*wr_cb_t)(u_register_t val, u_register_t *reg);
44
45/*
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000046 * Structure to hold the callback pointers for register access emulation.
Soby Mathewb4c6df42022-11-09 11:13:29 +000047 */
48struct sysreg_cb {
Soby Mathewb4c6df42022-11-09 11:13:29 +000049 rd_cb_t rd_cb;
50 wr_cb_t wr_cb;
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000051 /*
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 */
61struct sysreg_data {
62 char name[MAX_SYSREG_NAME_LEN + 1U];
63 struct sysreg_cb callbacks;
64 u_register_t value[MAX_CPUS];
Soby Mathewb4c6df42022-11-09 11:13:29 +000065};
66
67/*
68 * Return the callbacks for a given sysreg or NULL
69 * if no callbacks are found.
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000070 *
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 Mathewb4c6df42022-11-09 11:13:29 +000075 */
76struct 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 Sobrino9929a792022-11-22 16:11:13 +000085 * Arguments:
Soby Mathewb4c6df42022-11-09 11:13:29 +000086 * name - String containing the name of the sysreg. The name of
87 * the sysreg cannot exceed MAX_SYSREG_NAME_LEN (excluding
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000088 * the terminating NULL character) or it will be truncated.
Soby Mathewb4c6df42022-11-09 11:13:29 +000089 * 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 */
96int 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 Sobrino9929a792022-11-22 16:11:13 +0000108 * the terminating NULL character) or it will be truncated.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000109 * init - Value used as reset value for the sysreg.
110 *
111 * Returns:
112 * 0 on success or a negative error code otherwise.
113 */
114int host_util_set_default_sysreg_cb(char *name, u_register_t init);
115
116/*
Javier Almansa Sobrino48d68a72023-03-02 12:26:39 +0000117 * 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 Mathewb4c6df42022-11-09 11:13:29 +0000121 */
Javier Almansa Sobrino48d68a72023-03-02 12:26:39 +0000122void 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 */
128void host_util_restore_sysreg_snapshot(void);
129
130/*
131 * Zero all sysreg values and unregister all sysreg callbacks.
132 */
133void host_util_zero_sysregs_and_cbs(void);
Soby Mathewb4c6df42022-11-09 11:13:29 +0000134
135/*
136 * Return the configured address for the granule base.
137 */
138unsigned long host_util_get_granule_base(void);
139
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000140/*
141 * Set the current CPU emulated by the platform.
142 */
143void host_util_set_cpuid(unsigned int cpuid);
144
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100145/*
146 * Return the address of the EL3 RMM shared buffer.
147 */
148unsigned 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 */
154void host_util_setup_sysreg_and_boot_manifest(void);
155
Soby Mathewa694cca2023-04-24 03:42:45 +0100156/*
157 * Runs the realm entrypoint as programmed in elr_el2 and resets
158 * the esr_el2 before entering the Realm.
159 */
160int host_util_rec_run(unsigned long *regs);
161
162/* Prototype for Realm entrypoint */
163typedef int (*realm_entrypoint_t)(unsigned long *);
164
165/* Helper for invoking RSI calls */
166int host_util_rsi_helper(realm_entrypoint_t ep);
167
Soby Mathewb4c6df42022-11-09 11:13:29 +0000168#endif /* HOST_UTILS_H */