summaryrefslogtreecommitdiff
path: root/drivers/dsp/syslink/multicore_ipc/gatehwspinlock.c
blob: 57b53ad050e4932434cef2de21a7f5ee31c95ced (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
 *  gatehwspinlock.c
 *
 *  Hardware-based spinlock gate for mutual exclusion of shared memory.
 *
 *  Copyright (C) 2008-2009 Texas Instruments, Inc.
 *
 *  This package is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 *  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 *  WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE.
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <plat/hwspinlock.h>

#include <syslink/atomic_linux.h>
#include <multiproc.h>
#include <sharedregion.h>
#include <gatemp.h>
#include <igatempsupport.h>
#include <igateprovider.h>
#include <iobject.h>
#include <gatehwspinlock.h>


/* =============================================================================
 * Macros
 * =============================================================================
 */

/* Macro to make a correct module magic number with refCount */
#define GATEHWSPINLOCK_MAKE_MAGICSTAMP(x) ((GATEHWSPINLOCK_MODULEID << 12u)  \
					| (x))
/*
 *  structure for gatehwspinlock module state
 */
struct gatehwspinlock_module_object {
	atomic_t ref_count; /* Reference count */
	struct gatehwspinlock_config cfg;
	struct gatehwspinlock_config default_cfg;
	struct gatehwspinlock_params def_inst_params; /* default instance
							paramters */
	u32 *base_addr; /* Base address of lock registers */
	u32 num_locks; /* Maximum number of locks */
	u32 num_reserved; /* Number of reserved locks */
	struct hwspinlock **hw_lock_handles;
	/* Array of hwspinlock handles controlled by gatemp */
};

/*
 *  Structure defining object for the Gate Spinlock
 */
struct gatehwspinlock_object {
	IGATEPROVIDER_SUPEROBJECT; /* For inheritance from IGateProvider */
	IOBJECT_SUPEROBJECT;       /* For inheritance for IObject */
	u32 lock_num;
	u32 nested;
	void *local_gate;
	struct hwspinlock *hwhandle;
};

/*
 *  Variable for holding state of the gatehwspinlock module
 */
static struct gatehwspinlock_module_object gatehwspinlock_state = {
	.default_cfg.default_protection	= \
					gatehwspinlock_LOCALPROTECT_INTERRUPT,
	.default_cfg.num_locks		= 32u,
	.def_inst_params.shared_addr	= NULL,
	.def_inst_params.resource_id	= 0x0,
	.def_inst_params.region_id	= 0x0,
	.hw_lock_handles		= NULL,
	.num_locks			= 32u,
	.num_reserved			= 5
	/* GateMP controls SpinLocks 5-31. 0-3 are reserved for usage by I2C */
	/* SpinLock 4 is for ipu_pm module usage */
};

static struct gatehwspinlock_module_object *gatehwspinlock_module =
						&gatehwspinlock_state;

/* =============================================================================
 * Internal functions
 * =============================================================================
 */

/* TODO: figure these out */
#define gate_enter_system() (int *)0
#define gate_leave_system(key) {}

/* =============================================================================
 * APIS
 * =============================================================================
 */
/*
 * ======== gatehwspinlock_get_config ========
 *  Purpose:
 *  This will get the default configuration parameters for gatehwspinlock
 *  module
 */
void gatehwspinlock_get_config(struct gatehwspinlock_config *config)
{
	int *key = NULL;

	if (WARN_ON(config == NULL))
		goto exit;

	key = gate_enter_system();
	if (atomic_cmpmask_and_lt(&(gatehwspinlock_module->ref_count),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) == true)
		memcpy(config, &gatehwspinlock_module->default_cfg,
					sizeof(struct gatehwspinlock_config));
	else
		memcpy(config, &gatehwspinlock_module->cfg,
					sizeof(struct gatehwspinlock_config));
	gate_leave_system(key);

exit:
	return;
}
EXPORT_SYMBOL(gatehwspinlock_get_config);

/*
 * ======== gatehwspinlock_setup ========
 *  Purpose:
 *  This will setup the gatehwspinlock module
 */
int gatehwspinlock_setup(const struct gatehwspinlock_config *config)
{
	struct gatehwspinlock_config tmp_cfg;
	int *key = NULL;
	struct hwspinlock *lock_handle;
	int i;
	s32 retval;

	key = gate_enter_system();

	/* This sets the ref_count variable  not initialized, upper 16 bits is
	* written with module _id to ensure correctness of ref_count variable
	*/
	atomic_cmpmask_and_set(&gatehwspinlock_module->ref_count,
					GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
					GATEHWSPINLOCK_MAKE_MAGICSTAMP(0));

	if (atomic_inc_return(&gatehwspinlock_module->ref_count)
					!= GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) {
		gate_leave_system(key);
		return 1;
	}

	if (config == NULL) {
		gatehwspinlock_get_config(&tmp_cfg);
		config = &tmp_cfg;
	}
	gate_leave_system(key);

	gatehwspinlock_module->hw_lock_handles = kzalloc(
		gatehwspinlock_module->num_locks * sizeof(struct hwspinlock *),
		GFP_KERNEL);
	if (gatehwspinlock_module->hw_lock_handles == NULL) {
		retval = -ENOMEM;
		goto exit;
	}

	memcpy(&gatehwspinlock_module->cfg, config,
					sizeof(struct gatehwspinlock_config));

	gatehwspinlock_module->base_addr = (void *)config->base_addr;
	gatehwspinlock_module->num_locks = config->num_locks;
	for (i = gatehwspinlock_module->num_reserved;
		i < gatehwspinlock_module->num_locks; i++) {
			lock_handle = hwspinlock_request_specific(i);
			if (lock_handle == NULL) {
				retval = -EBUSY;
				pr_err("hwspinlock_request failed for"
					"id = %d", i);
				goto spinlock_request_fail;
			}
			gatehwspinlock_module->hw_lock_handles[i] = lock_handle;
	}
	return 0;

spinlock_request_fail:
	for (i--; i >= gatehwspinlock_module->num_reserved; i--) {
		hwspinlock_free(gatehwspinlock_module->hw_lock_handles[i]);
		gatehwspinlock_module->hw_lock_handles[i] = NULL;
	}
exit:
	kfree(gatehwspinlock_module->hw_lock_handles);
	atomic_dec_return(&gatehwspinlock_module->ref_count);
	if (retval < 0)
		pr_err("gatehwspinlock_setup failed! status = 0x%x", retval);
	return retval;
}
EXPORT_SYMBOL(gatehwspinlock_setup);

/*
 * ======== gatehwspinlock_destroy ========
 *  Purpose:
 *  This will destroy the gatehwspinlock module
 */
int gatehwspinlock_destroy(void)
{
	s32 retval = 0;
	int *key = NULL;
	struct hwspinlock *lock_handle;
	int i;

	key = gate_enter_system();

	if (atomic_cmpmask_and_lt(&(gatehwspinlock_module->ref_count),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto exit;
	}

	if (!(atomic_dec_return(&gatehwspinlock_module->ref_count)
			== GATEHWSPINLOCK_MAKE_MAGICSTAMP(0))) {
		gate_leave_system(key);
		retval = 1;
		goto exit;
	}
	gate_leave_system(key);

	for (i = gatehwspinlock_module->num_reserved;
		i < gatehwspinlock_module->num_locks; i++) {
			lock_handle = gatehwspinlock_module->hw_lock_handles[i];
			retval = hwspinlock_free(lock_handle);
			if (retval < 0)
				pr_err("hwspinlock_free failed for id = %d", i);
			gatehwspinlock_module->hw_lock_handles[i] = NULL;
	}
	memset(&gatehwspinlock_module->cfg, 0,
				sizeof(struct gatehwspinlock_config));
	kfree(gatehwspinlock_module->hw_lock_handles);
	gatehwspinlock_module->hw_lock_handles = NULL;
	return 0;

exit:
	if (retval < 0)
		pr_err("gatehwspinlock_destroy failed status:%x\n", retval);
	return retval;
}
EXPORT_SYMBOL(gatehwspinlock_destroy);

/*
 * ======== gatehwspinlock_get_num_instances ========
 *  Purpose:
 *  Function to return the number of instances configured in the module.
 */
u32 gatehwspinlock_get_num_instances(void)
{
	return gatehwspinlock_module->num_locks;
}
EXPORT_SYMBOL(gatehwspinlock_get_num_instances);

/*
 * ======== gatehwspinlock_get_num_reserved ========
 *  Purpose:
 *  Function to return the number of instances not under GateMP's control.
 */
u32 gatehwspinlock_get_num_reserved(void)
{
	return gatehwspinlock_module->num_reserved;
}
EXPORT_SYMBOL(gatehwspinlock_get_num_reserved);

/*
 * ======== gatepeterson_locks_init ========
 *  Purpose:
 *  Function to initialize the locks.
 */
void gatehwspinlock_locks_init(void)
{
	u32 i;

	for (i = 0; i < gatehwspinlock_module->num_locks; i++)
		gatehwspinlock_module->base_addr[i] = 0;
}
EXPORT_SYMBOL(gatehwspinlock_locks_init);

/*
 * ======== gatehwspinlock_params_init ========
 *  Purpose:
 *  This will  Initialize this config-params structure with
 *  supplier-specified defaults before instance creation
 */
void gatehwspinlock_params_init(struct gatehwspinlock_params *params)
{
	int *key = NULL;

	key = gate_enter_system();

	if (WARN_ON(atomic_cmpmask_and_lt(&(gatehwspinlock_module->ref_count),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) == true))
		goto exit;
	if (WARN_ON(params == NULL))
		goto exit;

	gate_leave_system(key);
	memcpy(params, &(gatehwspinlock_module->def_inst_params),
				sizeof(struct gatehwspinlock_params));
	return;

exit:
	gate_leave_system(key);
	return;
}
EXPORT_SYMBOL(gatehwspinlock_params_init);

/*
 * ======== gatehwspinlock_create ========
 *  Purpose:
 *  This will creates a new instance of gatehwspinlock module
 */
void *gatehwspinlock_create(enum igatempsupport_local_protect local_protect,
				const struct gatehwspinlock_params *params)
{
	void *handle = NULL;
	struct gatehwspinlock_object *obj = NULL;
	s32 retval = 0;

	if (atomic_cmpmask_and_lt(&(gatehwspinlock_module->ref_count),
					GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto exit;
	}
	if (WARN_ON(params == NULL)) {
		retval = -EINVAL;
		goto exit;
	}
	if (WARN_ON(params->shared_addr == NULL)) {
		retval = -EINVAL;
		goto exit;
	}

	obj = kzalloc(sizeof(struct gatehwspinlock_object), GFP_KERNEL);
	if (obj == NULL) {
		retval = -ENOMEM;
		goto exit;
	}

	IGATEPROVIDER_OBJECTINITIALIZER(obj, gatehwspinlock);

	/* Create the local gate */
	obj->local_gate =
		gatemp_create_local((enum gatemp_local_protect)local_protect);
	if (obj->local_gate == NULL) {
		retval = GATEHWSPINLOCK_E_FAIL;
		goto free_obj;
	}

	obj->lock_num = params->resource_id;
	obj->nested   = 0;
	obj->hwhandle = \
		gatehwspinlock_module->hw_lock_handles[params->resource_id];
	if (obj->hwhandle == NULL) {
		retval = -EBUSY;
		pr_err("hwspinlock_request failed for id = %d",
			params->resource_id);
		goto free_obj;
	}
	handle = obj;
	return handle;

free_obj:
	kfree(obj);
exit:
	pr_err("gatehwspinlock_create failed status: %x\n", retval);
	return NULL;
}
EXPORT_SYMBOL(gatehwspinlock_create);

/*
 * ======== gatehwspinlock_delete ========
 *  Purpose:
 *  This will deletes an instance of gatehwspinlock module
 */
int gatehwspinlock_delete(void **gphandle)

{
	struct gatehwspinlock_object *obj = NULL;
	s32 retval;

	if (atomic_cmpmask_and_lt(&(gatehwspinlock_module->ref_count),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) == true) {
		retval = -ENODEV;
		goto exit;
	}
	if (WARN_ON(gphandle == NULL)) {
		retval = -EINVAL;
		goto exit;
	}
	if (WARN_ON(*gphandle == NULL)) {
		retval = -EINVAL;
		goto exit;
	}

	obj = (struct gatehwspinlock_object *)(*gphandle);

	/* No need to delete the local gate, as it is gatemp module wide
	 * local mutex. */
	obj->hwhandle = NULL;
	kfree(obj);
	*gphandle = NULL;

	return 0;

exit:
	pr_err("gatehwspinlock_delete failed status: %x\n", retval);
	return retval;
}
EXPORT_SYMBOL(gatehwspinlock_delete);


/*
 * ======== gatehwspinlock_enter ========
 *  Purpose:
 *  This will enters the gatehwspinlock instance
 */
int *gatehwspinlock_enter(void *gphandle)
{
	struct gatehwspinlock_object *obj = NULL;
	s32 retval = 0;
	int *key = NULL;

	if (WARN_ON(atomic_cmpmask_and_lt(&(gatehwspinlock_module->ref_count),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) == true)) {
		retval = -ENODEV;
		goto exit;
	}
	if (WARN_ON(gphandle == NULL)) {
		retval = -EINVAL;
		goto exit;
	}

	obj = (struct gatehwspinlock_object *)gphandle;

	/* Enter local gate */
	if (obj->local_gate != NULL) {
		retval = mutex_lock_interruptible(
					(struct mutex *)obj->local_gate);
		if (retval)
			goto exit;
	}

	/* If the gate object has already been entered, return the nested
	 * value */
	obj->nested++;
	if (obj->nested > 1)
		return key;

	/* Enter the spinlock */
	retval = hwspinlock_lock(obj->hwhandle);
	if (retval < 0) {
		obj->nested--;
		mutex_unlock((struct mutex *)obj->local_gate);
	}

exit:
	if (retval < 0)
		pr_err("gatehwspinlock_enter failed! status = 0x%x", retval);
	return key;
}
EXPORT_SYMBOL(gatehwspinlock_enter);

/*
 * ======== gatehwspinlock_leave ========
 *  Purpose:
 *  This will leaves the gatehwspinlock instance
 */
void gatehwspinlock_leave(void *gphandle, int *key)
{
	struct gatehwspinlock_object *obj = NULL;
	s32 retval = 0;

	if (WARN_ON(atomic_cmpmask_and_lt(&(gatehwspinlock_module->ref_count),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) == true)) {
		retval = -ENODEV;
		goto exit;
	}
	if (WARN_ON(gphandle == NULL)) {
		retval = -EINVAL;
		goto exit;
	}

	obj = (struct gatehwspinlock_object *)gphandle;
	obj->nested--;
	/* Leave the spinlock if the leave() is not nested */
	if (obj->nested == 0) {
		retval = hwspinlock_unlock(obj->hwhandle);
		if (retval < 0) {
			obj->nested++;
			goto exit;
		}
	}
	/* Leave local gate */
	mutex_unlock(obj->local_gate);

exit:
	if (retval < 0)
		pr_err("gatehwspinlock_leave failed! status = 0x%x", retval);
	return;
}
EXPORT_SYMBOL(gatehwspinlock_leave);

/*
 *  ======== gatehwspinlock_get_resource_id ========
 */
static u32 gatehwspinlock_get_resource_id(void *handle)
{
	struct gatehwspinlock_object *obj = NULL;
	s32 retval = 0;

	if (WARN_ON(atomic_cmpmask_and_lt(&(gatehwspinlock_module->ref_count),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(0),
				GATEHWSPINLOCK_MAKE_MAGICSTAMP(1)) == true)) {
		retval = -ENODEV;
		goto exit;
	}
	if (WARN_ON(handle == NULL)) {
		retval = -EINVAL;
		goto exit;
	}

	obj = (struct gatehwspinlock_object *)handle;

	return obj->lock_num;

exit:
	pr_err("gatehwspinlock_get_resource_id failed status: %x\n", retval);
	return (u32)-1;
}
EXPORT_SYMBOL(gatehwspinlock_get_resource_id);

/*
 * ======== gatehwspinlock_shared_memreq ========
 *  Purpose:
 *  This will give the amount of shared memory required
 *  for creation of each instance
 */
u32 gatehwspinlock_shared_mem_req(const struct gatehwspinlock_params *params)
{
	return 0;
}
EXPORT_SYMBOL(gatehwspinlock_shared_mem_req);