blob: 8f69486b73625b173677fc4f5f46c857cdd0b41a [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
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 */
30typedef 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 */
42typedef void (*wr_cb_t)(u_register_t val, u_register_t *reg);
43
44/*
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000045 * Structure to hold the callback pointers for register access emulation.
Soby Mathewb4c6df42022-11-09 11:13:29 +000046 */
47struct sysreg_cb {
Soby Mathewb4c6df42022-11-09 11:13:29 +000048 rd_cb_t rd_cb;
49 wr_cb_t wr_cb;
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000050 /*
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 */
60struct sysreg_data {
61 char name[MAX_SYSREG_NAME_LEN + 1U];
62 struct sysreg_cb callbacks;
63 u_register_t value[MAX_CPUS];
Soby Mathewb4c6df42022-11-09 11:13:29 +000064};
65
66/*
67 * Return the callbacks for a given sysreg or NULL
68 * if no callbacks are found.
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000069 *
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 Mathewb4c6df42022-11-09 11:13:29 +000074 */
75struct 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 Sobrino9929a792022-11-22 16:11:13 +000084 * Arguments:
Soby Mathewb4c6df42022-11-09 11:13:29 +000085 * name - String containing the name of the sysreg. The name of
86 * the sysreg cannot exceed MAX_SYSREG_NAME_LEN (excluding
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +000087 * the terminating NULL character) or it will be truncated.
Soby Mathewb4c6df42022-11-09 11:13:29 +000088 * 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 */
95int 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 Sobrino9929a792022-11-22 16:11:13 +0000107 * the terminating NULL character) or it will be truncated.
Soby Mathewb4c6df42022-11-09 11:13:29 +0000108 * init - Value used as reset value for the sysreg.
109 *
110 * Returns:
111 * 0 on success or a negative error code otherwise.
112 */
113int host_util_set_default_sysreg_cb(char *name, u_register_t init);
114
115/*
116 * Clear the list of sysreg callbacks.
117 */
118void host_util_reset_all_sysreg_cb(void);
119
120/*
121 * Return the configured address for the granule base.
122 */
123unsigned long host_util_get_granule_base(void);
124
Javier Almansa Sobrino9929a792022-11-22 16:11:13 +0000125/*
126 * Set the current CPU emulated by the platform.
127 */
128void host_util_set_cpuid(unsigned int cpuid);
129
Mate Toth-Palaead06f2023-03-02 10:17:09 +0100130/*
131 * Return the address of the EL3 RMM shared buffer.
132 */
133unsigned 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 */
139void host_util_setup_sysreg_and_boot_manifest(void);
140
Soby Mathewb4c6df42022-11-09 11:13:29 +0000141#endif /* HOST_UTILS_H */