aboutsummaryrefslogtreecommitdiffstats
path: root/base/src/host/lib/mpu_cell.c
blob: 49744fddd7e5ae7f378474cce2843b9a1a19f38d (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright 2008 Sony Corporation of America
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this Library and associated documentation files (the
 * "Library"), to deal in the Library without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Library, and to
 * permit persons to whom the Library is furnished to do so, subject to
 * the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Library.
 *
 *  If you modify the Library, you may copy and distribute your modified
 *  version of the Library in object code or as an executable provided
 *  that you also do one of the following:
 *
 *   Accompany the modified version of the Library with the complete
 *   corresponding machine-readable source code for the modified version
 *   of the Library; or,
 *
 *   Accompany the modified version of the Library with a written offer
 *   for a complete machine-readable copy of the corresponding source
 *   code of the modified version of the Library.
 *
 *
 * THE LIBRARY IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
 */

#include <string.h>
#include <libspe2.h>

#include "config.h"

#include "mars/base.h"
#include "mars/context.h"
#include "mars/error.h"

#include "context_internal.h"
#include "workload_internal_types.h"
#include "numa_internal.h"

#ifdef MARS_ENABLE_NUMA

static int numa_mpu_max(void)
{
	int i;
	int max_node = numa_max_node();
	numa_bitmask_t mask = numa_get_run_node_mask();
	int num = 0;

	for (i = 0; i <= max_node; i++) {
		int ret;
		if (numa_bitmask_isbitset(mask, i)) {
			/* this call assumes assignment of
			 * each node number is identical
			 * between numa API and libspe.
			 */
			ret = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, i);
			if (ret < 0) {
				num = ret;
				break;
			}
			num += ret;
		}
	}
	numa_bitmask_free(mask);

	return num;
}

#else /* ! MARS_ENABLE_NUMA */

/* This function must not be called */
static int numa_mpu_max(void)
{
	return -1;
}

#endif /* ! MARS_ENABLE_NUMA */

extern const unsigned char mars_kernel_entry[];

int mars_mpu_max(uint32_t *num)
{
	int mpu_max;

	if (mars_numa_enabled())
		mpu_max = numa_mpu_max();
	else
		mpu_max = spe_cpu_info_get(SPE_COUNT_USABLE_SPES, -1);

	if (mpu_max < 0)
		return MARS_ERROR_INTERNAL;

	*num = mpu_max;

	return MARS_SUCCESS;
}

#ifdef ENABLE_COND_WAIT_FUTEX

static void *mpu_handler_thread(void *arg)
{
	int ret;
	spe_context_ptr_t spe = (spe_context_ptr_t)arg;

	while (1) {
  		unsigned int ea_h, ea_l;
		uint64_t ea;

		ret = spe_out_intr_mbox_read(spe, &ea_l, 1,
					     SPE_MBOX_ANY_BLOCKING);
		if (ret == 0)
			goto error;
		else if (ret == -1)
			continue;

		do {
			ret = spe_out_mbox_read(spe, &ea_h, 1);
			if (ret == -1 && errno != EINTR)
				goto error;
		} while (ret != 1);

		ea = ((uint64_t)ea_h << 32) | ea_l;
		if (ea == MARS_HOST_SIGNAL_EXIT)
			break;

		mars_ea_cond_signal(ea, 1);
	}

	return (void *)(uintptr_t)MARS_SUCCESS;

error:
	return (void *)(uintptr_t)MARS_ERROR_INTERNAL;
}

static int handler_thread_create(pthread_t *thread, spe_context_ptr_t spe)
{
	int ret;

	ret = pthread_create(thread, NULL, mpu_handler_thread, spe);
	if (ret)
		return MARS_ERROR_INTERNAL;

	return MARS_SUCCESS;
}

static int handler_thread_join(pthread_t thread)
{
	int ret;

	ret = pthread_join(thread, NULL);
	if (ret)
		return MARS_ERROR_INTERNAL;

	return MARS_SUCCESS;
}

#else /* !ENABLE_COND_WAIT_FUTEX */

static int handler_thread_create(pthread_t *thread, spe_context_ptr_t spe)
{
	(void)thread;

	return MARS_SUCCESS;
}

static int handler_thread_join(pthread_t thread)
{
	(void)thread;

	return MARS_SUCCESS;
}

#endif /* !ENABLE_COND_WAIT_FUTEX */

static void *mpu_context_thread(void *arg)
{
	int ret;
	unsigned int entry = SPE_DEFAULT_ENTRY;
	struct mars_kernel_params *params = (struct mars_kernel_params *)arg;
	spe_context_ptr_t spe;
	spe_program_handle_t prog;
	pthread_t handler_thread;

	spe = spe_context_create(0, NULL);
	if (!spe)
		return (void *)MARS_ERROR_INTERNAL;

	memset(&prog, 0, sizeof(prog));
	prog.handle_size = sizeof(prog);
	prog.elf_image = (void *)mars_kernel_entry;
	ret = spe_program_load(spe, &prog);
	if (ret) {
		spe_context_destroy(spe);
		return (void *)MARS_ERROR_INTERNAL;
	}

	ret = handler_thread_create(&handler_thread, spe);
	if (ret) {
		spe_context_destroy(spe);
		return (void *)ret;
	}

	ret = spe_context_run(spe, &entry, 0, params, NULL, NULL);
	if (ret) {
		spe_context_destroy(spe);
		return (void *)MARS_ERROR_INTERNAL;
	}

	handler_thread_join(handler_thread);

	ret = spe_context_destroy(spe);
	if (ret)
		return (void *)MARS_ERROR_INTERNAL;

	return (void *)MARS_SUCCESS;
}

int mars_mpu_run(mars_mpu_context_t *mpu, uint64_t params_ea)
{
	int ret;

	ret = pthread_create(mpu, NULL, mpu_context_thread,
			     (void *)(uintptr_t)params_ea);
	if (ret)
		return MARS_ERROR_INTERNAL;

	return MARS_SUCCESS;
}

int mars_mpu_wait(mars_mpu_context_t *mpu)
{
	int ret;
	void *p_ret;

	ret = pthread_join(*mpu, &p_ret);
	if (ret || p_ret)
		return MARS_ERROR_INTERNAL;

	return MARS_SUCCESS;
}