aboutsummaryrefslogtreecommitdiffstats
path: root/base/src/host/lib/context.c
blob: 22dbbc955a8843a784a2c5312df5e2b558eb4a28 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * 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 <ppu_intrinsics.h>

#include "config.h"

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

#include "callback_internal_types.h"
#include "kernel_internal_types.h"
#include "workload_internal_types.h"

#include "context_internal.h"

uint32_t mars_get_ticks(void)
{
	return __mftb() & 0xffffffff;
}

static void kernel_ticks_sync(uint64_t kernel_ticks_ea)
{
	uint64_t flag_ea =
		kernel_ticks_ea + offsetof(struct mars_kernel_ticks, flag);
	uint64_t offset_ea =
		kernel_ticks_ea + offsetof(struct mars_kernel_ticks, offset);

	/* wait until kernel sets the sync begin flag */
	do {
	} while (mars_ea_get_uint32(flag_ea) !=
		 MARS_KERNEL_TICKS_FLAG_SYNC_BEGIN);

	mars_ea_sync();

	mars_ea_put_uint32(offset_ea, mars_get_ticks());

	mars_ea_sync();

	/* set the sync end flag so kernel can finish sync */
	mars_ea_put_uint32(flag_ea, MARS_KERNEL_TICKS_FLAG_SYNC_END);
}

static int kernel_params_init(struct mars_context *mars, uint64_t params_ea,
			      uint16_t kernel_id)
{
	struct mars_kernel_params *params =
		mars_ea_work_area_get(params_ea,
				      MARS_KERNEL_PARAMS_ALIGN,
				      MARS_KERNEL_PARAMS_SIZE);

	if (!params)
		return MARS_ERROR_MEMORY;

	/* zero kernel params */
	memset(params, 0, MARS_KERNEL_PARAMS_SIZE);

	params->kernel_id = kernel_id;
	params->mars_context_ea = mars_ptr_to_ea(mars);
	params->workload_queue_ea = mars->workload_queue_ea;
	params->callback_queue_ea = mars->callback_queue_ea;

	/* update params on EA */
	mars_ea_put(params_ea, params, MARS_KERNEL_PARAMS_SIZE);
	mars_ea_sync();

	return MARS_SUCCESS;
}

static int mpu_contexts_create(struct mars_context *mars, uint32_t num_mpus)
{
	int ret;
	uint16_t i;

	/* create threads for each mpu context */
	for (i = mars->mpu_context_count; i < num_mpus; i++) {
		uint64_t params_ea = mars->kernel_params_ea +
				     MARS_KERNEL_PARAMS_SIZE * i;

		/* initialize kernel params for current mpu context */
		ret = kernel_params_init(mars, params_ea, i);
		if (ret != MARS_SUCCESS)
			return ret;

		/* run current mpu context */
		ret = mars_mpu_run(&mars->mpu_contexts[i], params_ea);
		if (ret != MARS_SUCCESS)
			return ret;

		/* sync kernel ticks for current mpu context */
		kernel_ticks_sync(params_ea +
			offsetof(struct mars_kernel_params, kernel_ticks));

		/* increment mars context mpu context count */
		mars->mpu_context_count++;
	}

	return MARS_SUCCESS;
}

static int mpu_contexts_destroy(struct mars_context *mars)
{
	int ret;
	uint16_t i;

	/* destroy all mpu contexts */
	for (i = 0; i < mars->mpu_context_count; i++) {
		/* wait for mpu context threads */
		ret = mars_mpu_wait(&mars->mpu_contexts[i]);
		if (ret != MARS_SUCCESS)
			return ret;
	}

	return MARS_SUCCESS;
}

static int system_sanity_check(void)
{
	if ((MARS_CALLBACK_ARGS_SIZE !=
		sizeof(struct mars_callback_args)) ||
	    (MARS_CALLBACK_QUEUE_SIZE !=
		sizeof(struct mars_callback_queue)) ||
	    (MARS_KERNEL_PARAMS_SIZE !=
		sizeof(struct mars_kernel_params)) ||
	    (MARS_MUTEX_SIZE !=
		sizeof(struct mars_mutex)) ||
	    (MARS_WORKLOAD_CALLBACK_SIZE !=
		sizeof(struct mars_workload_callback)) ||
	    (MARS_WORKLOAD_CONTEXT_SIZE !=
		sizeof(struct mars_workload_context)) ||
	    (MARS_WORKLOAD_MODULE_SIZE !=
		sizeof(struct mars_workload_module)) ||
	    (MARS_WORKLOAD_QUEUE_SIZE !=
		sizeof(struct mars_workload_queue)) ||
	    (MARS_WORKLOAD_QUEUE_HEADER_SIZE !=
		sizeof(struct mars_workload_queue_header)) ||
	    (MARS_WORKLOAD_QUEUE_BLOCK_SIZE !=
		sizeof(struct mars_workload_queue_block)))
		return MARS_ERROR_INTERNAL;

	return MARS_SUCCESS;
}

int mars_context_create(struct mars_context **mars_ret, uint32_t num_mpus,
			uint8_t shared)
{
	int ret;
	uint32_t num_mpus_max;
	struct mars_context *mars = NULL;

	/* check function params */
	if (!mars_ret)
		return MARS_ERROR_NULL;

	/* check number of mpus to use */
	ret = mars_mpu_max(&num_mpus_max);
	if (ret != MARS_SUCCESS)
		return ret;
	if (!num_mpus_max)
		return MARS_ERROR_LIMIT;
	if (num_mpus > num_mpus_max)
		return MARS_ERROR_PARAMS;
	if (!num_mpus)
		num_mpus = num_mpus_max;

	/* system sanity check */
	ret = system_sanity_check();
	if (ret != MARS_SUCCESS)
		return ret;

	/* lock mutex */
	ret = mars_host_mutex_lock(&mars_shared_context_lock);
	if (ret != MARS_SUCCESS)
		return ret;

	/* shared context requested */
	if (shared && mars_shared_context_get(&mars) == MARS_SUCCESS) {
		/* create any extra mpu contexts necessary */
		ret = mpu_contexts_create(mars, num_mpus);
		if (ret != MARS_SUCCESS)
			goto error;

		/* increment shared context reference count */
		mars->reference_count++;

		/* return the shared context */
		*mars_ret = mars;

		goto done;
	}

	/* allocate context */
	mars = mars_malloc(sizeof(struct mars_context));
	if (!mars) {
		ret = MARS_ERROR_MEMORY;
		goto error;
	}

	/* zero context */
	memset(mars, 0, sizeof(struct mars_context));

	/* increment reference count */
	mars->reference_count++;

	/* allocate kernel params */
	mars->kernel_params_ea = mars_ea_memalign(
			MARS_KERNEL_PARAMS_ALIGN,
			MARS_KERNEL_PARAMS_SIZE * num_mpus_max);
	if (!mars->kernel_params_ea) {
		ret = MARS_ERROR_MEMORY;
		goto error_malloc_kernel_params;
	}

	/* allocate mpu context thread array */
	mars->mpu_contexts = (mars_mpu_context_t *)
		mars_malloc(sizeof(mars_mpu_context_t) * num_mpus_max);
	if (!mars->mpu_contexts) {
		ret = MARS_ERROR_MEMORY;
		goto error_malloc_mpu_contexts;
	}

	/* create workload queue */
	ret = mars_workload_queue_create(mars);
	if (ret != MARS_SUCCESS)
		goto error_workload_queue_create;

	/* create callback queue */
	ret = mars_callback_queue_create(mars);
	if (ret != MARS_SUCCESS)
		goto error_callback_queue_create;

	/* create mpu contexts */
	ret = mpu_contexts_create(mars, num_mpus);
	if (ret != MARS_SUCCESS)
		goto error_mpu_contexts_create;

	/* set the shared context pointer */
	if (shared) {
		ret = mars_shared_context_register(mars);
		if (ret != MARS_SUCCESS)
			goto error_shared_context_unlock;
	}

	/* return mars context pointer */
	*mars_ret = mars;

done:
	/* unlock mutex */
	ret = mars_host_mutex_unlock(&mars_shared_context_lock);
	if (ret != MARS_SUCCESS)
		goto error_shared_context_unlock;

	return MARS_SUCCESS;

error_shared_context_unlock:
	mpu_contexts_destroy(mars);
error_mpu_contexts_create:
	mars_callback_queue_destroy(mars);
error_callback_queue_create:
	mars_workload_queue_destroy(mars);
error_workload_queue_create:
	mars_free(mars->mpu_contexts);
error_malloc_mpu_contexts:
	mars_ea_free(mars->kernel_params_ea);
error_malloc_kernel_params:
	mars_free(mars);
error:
	mars_host_mutex_unlock(&mars_shared_context_lock);

	return ret;
}

int mars_context_destroy(struct mars_context *mars)
{
	int ret;

	/* check function params */
	if (!mars)
		return MARS_ERROR_NULL;

	/* lock mutex */
	ret = mars_host_mutex_lock(&mars_shared_context_lock);
	if (ret != MARS_SUCCESS)
		return ret;

	/* decrement reference count */
	mars->reference_count--;

	/* reference count is not 0 so return */
	if (mars->reference_count)
		goto done;

	/* shutdown the workload queue so mpu context threads exit */
	ret = mars_workload_queue_exit(mars);
	if (ret != MARS_SUCCESS)
		return ret;

	/* shutdown the callback queue so callback handler threads exit */
	ret = mars_callback_queue_exit(mars);
	if (ret != MARS_SUCCESS)
		return ret;

	/* destroy mpu contexts */
	if (mars->mpu_context_count) {
		ret = mpu_contexts_destroy(mars);
		if (ret != MARS_SUCCESS)
			goto error;
	}

	/* destroy callback queue */
	if (mars->callback_queue_ea) {
		ret = mars_callback_queue_destroy(mars);
		if (ret != MARS_SUCCESS)
			goto error;
	}

	/* destroy workload queue */
	if (mars->workload_queue_ea) {
		ret = mars_workload_queue_destroy(mars);
		if (ret != MARS_SUCCESS)
			goto error;
	}

	/* free allocated memory */
	mars_free(mars->mpu_contexts);
	mars_ea_free(mars->kernel_params_ea);
	mars_free(mars);

	/* unregister shared context */
	mars_shared_context_unregister(mars);

done:
	/* unlock mutex */
	ret = mars_host_mutex_unlock(&mars_shared_context_lock);
	if (ret != MARS_SUCCESS)
		return ret;

	return MARS_SUCCESS;

error:
	mars_host_mutex_unlock(&mars_shared_context_lock);

	return ret;
}