aboutsummaryrefslogtreecommitdiff
path: root/smc_fuzz/src/fifo3d.c
blob: 119b26c7e8f71212440f7f041b53369152adf2e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (c) 2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <debug.h>
#include <drivers/arm/private_timer.h>
#include <events.h>
#include "fifo3d.h"
#include <libfdt.h>

#include <power_management.h>
#include <sdei.h>
#include <tftf_lib.h>
#include <timer.h>

#include <plat_topology.h>
#include <platform.h>

#ifdef SMC_FUZZ_TMALLOC
#define GENMALLOC(x)	malloc((x))
#define GENFREE(x)	free((x))
#else
#define GENMALLOC(x)	smcmalloc((x), mmod)
#define GENFREE(x)	smcfree((x), mmod)
#endif

/*
 * Push function name string into raw data structure
 */
void push_3dfifo_fname(struct fifo3d *f3d, char *fname)
{
	strlcpy(f3d->fnamefifo[f3d->col - 1][f3d->row[f3d->col - 1] - 1],
		fname, MAX_NAME_CHARS);
}

/*
 * Push bias value into raw data structure
 */
void push_3dfifo_bias(struct fifo3d *f3d, int bias)
{
	f3d->biasfifo[f3d->col - 1][f3d->row[f3d->col - 1] - 1] = bias;
}

/*
 * Create new column and/or row for raw data structure for newly
 * found node from device tree
 */
void push_3dfifo_col(struct fifo3d *f3d, char *entry, struct memmod *mmod)
{
	char ***tnnfifo;
	char ***tfnamefifo;
	int **tbiasfifo;

	if (f3d->col == f3d->curr_col) {
		f3d->col++;
		f3d->curr_col++;
		int *trow;
		trow = GENMALLOC(f3d->col * sizeof(int));

		/*
		 * return if error found
		 */
		if (mmod->memerror != 0) {
			return;
		}

		for (unsigned int i = 0U; (int)i < f3d->col - 1; i++) {
			trow[i] = f3d->row[i];
		}
		if (f3d->col > 1) {
			GENFREE(f3d->row);
		}
		f3d->row = trow;
		f3d->row[f3d->col - 1] = 1;

		/*
		 * Create new raw data memory
		 */
		tnnfifo = GENMALLOC(f3d->col * sizeof(char **));
		tfnamefifo = GENMALLOC(f3d->col * sizeof(char **));
		tbiasfifo = GENMALLOC((f3d->col) * sizeof(int *));
		for (unsigned int i = 0U; (int)i < f3d->col; i++) {
			tnnfifo[i] = GENMALLOC(f3d->row[i] * sizeof(char *));
			tfnamefifo[i] = GENMALLOC(f3d->row[i] * sizeof(char *));
			tbiasfifo[i] = GENMALLOC((f3d->row[i]) * sizeof(int));
			for (unsigned int j = 0U; (int)j < f3d->row[i]; j++) {
				tnnfifo[i][j] = GENMALLOC(1 * sizeof(char[MAX_NAME_CHARS]));
				tfnamefifo[i][j] =
					GENMALLOC(1 * sizeof(char[MAX_NAME_CHARS]));
				if (!((j == f3d->row[f3d->col - 1] - 1) &&
				      (i == (f3d->col - 1)))) {
					strlcpy(tnnfifo[i][j], f3d->nnfifo[i][j], MAX_NAME_CHARS);
					strlcpy(tfnamefifo[i][j],
						f3d->fnamefifo[i][j], MAX_NAME_CHARS);
					tbiasfifo[i][j] = f3d->biasfifo[i][j];
				}
			}
		}

		/*
		 * Copy data from old raw data to new memory location
		 */
		strlcpy(tnnfifo[f3d->col - 1][f3d->row[f3d->col - 1] - 1], entry,
			MAX_NAME_CHARS);
		strlcpy(tfnamefifo[f3d->col - 1][f3d->row[f3d->col - 1] - 1],
			"none", MAX_NAME_CHARS);
		tbiasfifo[f3d->col - 1][f3d->row[f3d->col - 1] - 1] = 0;

		/*
		 * Free the old raw data structres
		 */
		for (unsigned int i = 0U; (int)i < f3d->col - 1; i++) {
			for (unsigned int j = 0U; (int)j < f3d->row[i]; j++) {
				GENFREE(f3d->nnfifo[i][j]);
				GENFREE(f3d->fnamefifo[i][j]);
			}
			GENFREE(f3d->nnfifo[i]);
			GENFREE(f3d->fnamefifo[i]);
			GENFREE(f3d->biasfifo[i]);
		}
		if (f3d->col > 1) {
			GENFREE(f3d->nnfifo);
			GENFREE(f3d->fnamefifo);
			GENFREE(f3d->biasfifo);
		}

		/*
		 * Point to new data
		 */
		f3d->nnfifo = tnnfifo;
		f3d->fnamefifo = tfnamefifo;
		f3d->biasfifo = tbiasfifo;
	}
	if (f3d->col != f3d->curr_col) {
		/*
		 *  Adding new node to raw data
		 */
		f3d->col++;
		f3d->row[f3d->col - 1]++;

		/*
		 * Create new raw data memory
		 */
		tnnfifo = GENMALLOC(f3d->col * sizeof(char **));
		tfnamefifo = GENMALLOC(f3d->col * sizeof(char **));
		tbiasfifo = GENMALLOC((f3d->col) * sizeof(int *));
		for (unsigned int i = 0U; (int)i < f3d->col; i++) {
			tnnfifo[i] = GENMALLOC(f3d->row[i] * sizeof(char *));
			tfnamefifo[i] = GENMALLOC(f3d->row[i] * sizeof(char *));
			tbiasfifo[i] = GENMALLOC((f3d->row[i]) * sizeof(int));
			for (unsigned int j = 0U; (int)j < f3d->row[i]; j++) {
				tnnfifo[i][j] = GENMALLOC(1 * sizeof(char[MAX_NAME_CHARS]));
				tfnamefifo[i][j] =
					GENMALLOC(1 * sizeof(char[MAX_NAME_CHARS]));
				if (!((j == f3d->row[f3d->col - 1] - 1) &&
				      (i == (f3d->col - 1)))) {
					strlcpy(tnnfifo[i][j], f3d->nnfifo[i][j], MAX_NAME_CHARS);
					strlcpy(tfnamefifo[i][j],
						f3d->fnamefifo[i][j], MAX_NAME_CHARS);
					tbiasfifo[i][j] = f3d->biasfifo[i][j];
				}
			}
		}

		/*
		 * Copy data from old raw data to new memory location
		 */
		strlcpy(tnnfifo[f3d->col - 1][f3d->row[f3d->col - 1] - 1], entry,
			MAX_NAME_CHARS);
		strlcpy(tfnamefifo[f3d->col - 1][f3d->row[f3d->col - 1] - 1],
			"none", MAX_NAME_CHARS);
		tbiasfifo[f3d->col - 1][f3d->row[f3d->col - 1] - 1] = 0;

		/*
		 * Free the old raw data structres
		 */
		for (unsigned int i = 0U; (int)i < f3d->col; i++) {
			for (unsigned int j = 0U; (int)j < f3d->row[i]; j++) {
				if (!((i == f3d->col - 1) &&
				      (j == f3d->row[i] - 1))) {
					GENFREE(f3d->nnfifo[i][j]);
					GENFREE(f3d->fnamefifo[i][j]);
				}
			}
			GENFREE(f3d->nnfifo[i]);
			GENFREE(f3d->fnamefifo[i]);
			GENFREE(f3d->biasfifo[i]);
		}
		GENFREE(f3d->nnfifo);
		GENFREE(f3d->fnamefifo);
		GENFREE(f3d->biasfifo);

		/*
		 * Point to new data
		 */
		f3d->nnfifo = tnnfifo;
		f3d->fnamefifo = tfnamefifo;
		f3d->biasfifo = tbiasfifo;
	}
}