aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ia64/sn/kernel/sv.c
blob: 0fa601f910038bec02f09d887d0f8eaba83a5b5a (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
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
 *
 * This implemenation of synchronization variables is heavily based on
 * one done by Steve Lord <lord@sgi.com>
 *
 * Paul Cassella <pwc@sgi.com>
 */

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/interrupt.h>

#include <asm/semaphore.h>
#include <asm/hardirq.h>
#include <asm/current.h>

#include <asm/sn/sv.h>

/* Define this to have sv_test() run some simple tests.
   kernel_thread() must behave as expected when this is called.  */
#undef RUN_SV_TEST

#define DEBUG

/* Set up some macros so sv_wait(), sv_signal(), and sv_broadcast()
   can sanity check interrupt state on architectures where we know
   how. */
#ifdef DEBUG
 #define SV_DEBUG_INTERRUPT_STATE
 #ifdef __mips64
  #define SV_TEST_INTERRUPTS_ENABLED(flags) ((flags & 0x1) != 0)
  #define SV_TEST_INTERRUPTS_DISABLED(flags) ((flags & 0x1) == 0)
  #define SV_INTERRUPT_TEST_WORKERS 31
 #elif defined(__ia64)
  #define SV_TEST_INTERRUPTS_ENABLED(flags) ((flags & 0x4000) != 0)
  #define SV_TEST_INTERRUPTS_DISABLED(flags) ((flags & 0x4000) == 0)
  #define SV_INTERRUPT_TEST_WORKERS 4 /* simulator's slow */
 #else
  #undef  SV_DEBUG_INTERRUPT_STATE
  #define SV_INTERRUPT_TEST_WORKERS 4 /* reasonable? default. */
 #endif /* __mips64 */
#endif /* DEBUG */


/* XXX FIXME hack hack hack.  Our mips64 tree is from before the
   switch to WQ_FLAG_EXCLUSIVE, and our ia64 tree is from after it. */
#ifdef TASK_EXCLUSIVE
  #undef EXCLUSIVE_IN_QUEUE
#else
  #define EXCLUSIVE_IN_QUEUE
  #define TASK_EXCLUSIVE 0 /* for the set_current_state() in sv_wait() */
#endif


static inline void sv_lock(sv_t *sv) {
	spin_lock(&sv->sv_lock);
}

static inline void sv_unlock(sv_t *sv) {
	spin_unlock(&sv->sv_lock);
}

/* up() is "extern inline", so we can't pass its address to sv_wait.
   Use this function's address instead. */
static void up_wrapper(struct semaphore *sem) {
	up(sem);
}

/* spin_unlock() is sometimes a macro. */
static void spin_unlock_wrapper(spinlock_t *s) {
	spin_unlock(s);
}

/* XXX Perhaps sv_wait() should do the switch() each time and avoid
   the extra indirection and the need for the _wrapper functions? */

static inline void sv_set_mon_type(sv_t *sv, int type) {
	switch (type) {
	case SV_MON_SPIN:
		sv->sv_mon_unlock_func =
		  (sv_mon_unlock_func_t)spin_unlock_wrapper;
		break;
	case SV_MON_SEMA:
		sv->sv_mon_unlock_func =
		  (sv_mon_unlock_func_t)up_wrapper;
		if(sv->sv_flags & SV_INTS) {
			printk(KERN_ERR "sv_set_mon_type: The monitor lock "
			       "cannot be shared with interrupts if it is a "
			       "semaphore!\n");
			BUG();
		}
		if(sv->sv_flags & SV_BHS) {
			printk(KERN_ERR "sv_set_mon_type: The monitor lock "
			       "cannot be shared with bottom-halves if it is "
			       "a semaphore!\n");
			BUG();
		}
		break;
#if 0 
	/*
	 * If needed, and will need to think about interrupts.  This
	 * may be needed, for example, if someone wants to use sv's
	 * with something like dev_base; writers need to hold two
	 * locks. 
	 */
	case SV_MON_CUSTOM: 
		{
		struct sv_mon_custom *c = lock;
		sv->sv_mon_unlock_func = c->sv_mon_unlock_func;
		sv->sv_mon_lock        = c->sv_mon_lock;
		break;
		}
#endif
		
	default:
		printk(KERN_ERR "sv_set_mon_type: unknown type %d (0x%x)! "
		       "(flags 0x%x)\n", type, type, sv->sv_flags);
		BUG();
		break;
	}
	sv->sv_flags |= type;
}

static inline void sv_set_ord(sv_t *sv, int ord) {
	if (!ord)
		ord = SV_ORDER_DEFAULT;

	if (ord != SV_ORDER_FIFO && ord != SV_ORDER_LIFO) {
		printk(KERN_EMERG "sv_set_ord: unknown order %d (0x%x)! ",
		       ord, ord);
		BUG();
	}

	sv->sv_flags |= ord;
}

void sv_init(sv_t *sv, sv_mon_lock_t *lock, int flags) 
{
	int ord = flags & SV_ORDER_MASK;
	int type = flags & SV_MON_MASK;

	/* Copy all non-order, non-type flags */
	sv->sv_flags = (flags & ~(SV_ORDER_MASK | SV_MON_MASK));

	if((sv->sv_flags & (SV_INTS | SV_BHS)) == (SV_INTS | SV_BHS)) {
	  printk(KERN_ERR "sv_init: do not set both SV_INTS and SV_BHS, only SV_INTS.\n");
	  BUG();
	}

	sv_set_ord(sv, ord);
	sv_set_mon_type(sv, type);

	/* If lock is NULL, we'll get it from sv_wait_compat() (and
           ignore it in sv_signal() and sv_broadcast()). */
	sv->sv_mon_lock = lock;

	spin_lock_init(&sv->sv_lock);
	init_waitqueue_head(&sv->sv_waiters);
}

/*
 * The associated lock must be locked on entry.  It is unlocked on return.
 *
 * Return values:
 *
 * n < 0 : interrupted,  -n jiffies remaining on timeout, or -1 if timeout == 0
 * n = 0 : timeout expired
 * n > 0 : sv_signal()'d, n jiffies remaining on timeout, or 1 if timeout == 0
 */
signed long sv_wait(sv_t *sv, int sv_wait_flags, unsigned long timeout) 
{
	DECLARE_WAITQUEUE( wait, current );
	unsigned long flags;
	signed long ret = 0;

#ifdef SV_DEBUG_INTERRUPT_STATE
	{
	unsigned long flags;
	local_save_flags(flags);

	if(sv->sv_flags & SV_INTS) {
		if(SV_TEST_INTERRUPTS_ENABLED(flags)) {
			printk(KERN_ERR "sv_wait: SV_INTS and interrupts "
			       "enabled (flags: 0x%lx)\n", flags);
			BUG();
		}
	} else {
		if (SV_TEST_INTERRUPTS_DISABLED(flags)) {
			printk(KERN_WARNING "sv_wait: !SV_INTS and interrupts "
			       "disabled! (flags: 0x%lx)\n", flags);
		}
	}
	}
#endif  /* SV_DEBUG_INTERRUPT_STATE */

	sv_lock(sv);

	sv->sv_mon_unlock_func(sv->sv_mon_lock);

	/* Add ourselves to the wait queue and set the state before
	 * releasing the sv_lock so as to avoid racing with the
	 * wake_up() in sv_signal() and sv_broadcast(). 
	 */

	/* don't need the _irqsave part, but there is no wq_write_lock() */
	write_lock_irqsave(&sv->sv_waiters.lock, flags);

#ifdef EXCLUSIVE_IN_QUEUE
	wait.flags |= WQ_FLAG_EXCLUSIVE;
#endif

	switch(sv->sv_flags & SV_ORDER_MASK) {
	case SV_ORDER_FIFO:
		__add_wait_queue_tail(&sv->sv_waiters, &wait);
		break;
	case SV_ORDER_FILO:
		__add_wait_queue(&sv->sv_waiters, &wait);
		break;
	default:
		printk(KERN_ERR "sv_wait: unknown order!  (sv: 0x%p, flags: 0x%x)\n",
					(void *)sv, sv->sv_flags);
		BUG();
	}
	write_unlock_irqrestore(&sv->sv_waiters.lock, flags);

	if(sv_wait_flags & SV_WAIT_SIG)
		set_current_state(TASK_EXCLUSIVE | TASK_INTERRUPTIBLE  );
	else
		set_current_state(TASK_EXCLUSIVE | TASK_UNINTERRUPTIBLE);

	spin_unlock(&sv->sv_lock);

	if(sv->sv_flags & SV_INTS)
		local_irq_enable();
	else if(sv->sv_flags & SV_BHS)
		local_bh_enable();

	if (timeout)
		ret = schedule_timeout(timeout);
	else
		schedule();

	if(current->state != TASK_RUNNING) /* XXX Is this possible? */ {
		printk(KERN_ERR "sv_wait: state not TASK_RUNNING after "
		       "schedule().\n");
		set_current_state(TASK_RUNNING);
	}

	remove_wait_queue(&sv->sv_waiters, &wait);

	/* Return cases:
	   - woken by a sv_signal/sv_broadcast
	   - woken by a signal
	   - woken by timeout expiring
	*/

	/* XXX This isn't really accurate; we may have been woken
           before the signal anyway.... */
	if(signal_pending(current))
		return timeout ? -ret : -1;
	return timeout ? ret : 1;
}


void sv_signal(sv_t *sv) 
{
	/* If interrupts can acquire this lock, they can also acquire the
	   sv_mon_lock, which we must already have to have called this, so
	   interrupts must be disabled already.  If interrupts cannot
	   contend for this lock, we don't have to worry about it. */

#ifdef SV_DEBUG_INTERRUPT_STATE
	if(sv->sv_flags & SV_INTS) {
		unsigned long flags;
		local_save_flags(flags);
		if(SV_TEST_INTERRUPTS_ENABLED(flags))
			printk(KERN_ERR "sv_signal: SV_INTS and "
			"interrupts enabled! (flags: 0x%lx)\n", flags);
	}
#endif /* SV_DEBUG_INTERRUPT_STATE */

	sv_lock(sv);
	wake_up(&sv->sv_waiters);
	sv_unlock(sv);
}

void sv_broadcast(sv_t *sv) 
{
#ifdef SV_DEBUG_INTERRUPT_STATE
	if(sv->sv_flags & SV_INTS) {
		unsigned long flags;
		local_save_flags(flags);
		if(SV_TEST_INTERRUPTS_ENABLED(flags))
			printk(KERN_ERR "sv_broadcast: SV_INTS and "
			       "interrupts enabled! (flags: 0x%lx)\n", flags);
	}
#endif /* SV_DEBUG_INTERRUPT_STATE */

	sv_lock(sv);
	wake_up_all(&sv->sv_waiters);
	sv_unlock(sv);
}

void sv_destroy(sv_t *sv) 
{
	if(!spin_trylock(&sv->sv_lock)) {
		printk(KERN_ERR "sv_destroy: someone else has sv 0x%p locked!\n", (void *)sv);
		BUG();
	}

	/* XXX Check that the waitqueue is empty? 
	       Mark the sv destroyed?
	*/
}


#ifdef RUN_SV_TEST

static DECLARE_MUTEX_LOCKED(talkback);
static DECLARE_MUTEX_LOCKED(sem);
sv_t sv;
sv_t sv_filo;

static int sv_test_1_w(void *arg) 
{
	printk("sv_test_1_w: acquiring spinlock 0x%p...\n", arg);

	spin_lock((spinlock_t*)arg);
	printk("sv_test_1_w: spinlock acquired, waking sv_test_1_s.\n");

	up(&sem);

	printk("sv_test_1_w: sv_spin_wait()'ing.\n");

	sv_spin_wait(&sv, arg);

	printk("sv_test_1_w: talkback.\n");
	up(&talkback);

	printk("sv_test_1_w: exiting.\n");
	return 0;
}

static int sv_test_1_s(void *arg) 
{
	printk("sv_test_1_s: waiting for semaphore.\n");
	down(&sem);
	printk("sv_test_1_s: semaphore acquired.  Acquiring spinlock.\n");
	spin_lock((spinlock_t*)arg);
	printk("sv_test_1_s: spinlock acquired.  sv_signaling.\n");
	sv_signal(&sv);
	printk("sv_test_1_s: talkback.\n");
	up(&talkback);
	printk("sv_test_1_s: exiting.\n");
	return 0;

}

static int count;
static DECLARE_MUTEX(monitor);

static int sv_test_2_w(void *arg) 
{
	int dummy = count++;
	sv_t *sv = (sv_t *)arg;

	down(&monitor);
	up(&talkback);
	printk("sv_test_2_w: thread %d started, sv_waiting.\n", dummy);
	sv_sema_wait(sv, &monitor);
	printk("sv_test_2_w: thread %d woken, exiting.\n", dummy);
	up(&sem);
	return 0;
}

static int sv_test_2_s_1(void *arg) 
{
	int i;
	sv_t *sv = (sv_t *)arg;

	down(&monitor);
	for(i = 0; i < 3; i++) {
		printk("sv_test_2_s_1: waking one thread.\n");
		sv_signal(sv);
		down(&sem);
	}

	printk("sv_test_2_s_1: signaling and broadcasting again.  Nothing should happen.\n");
	sv_signal(sv);
	sv_broadcast(sv);
	sv_signal(sv);
	sv_broadcast(sv);

	printk("sv_test_2_s_1: talkbacking.\n");
	up(&talkback);
	up(&monitor);
	return 0;
}

static int sv_test_2_s(void *arg) 
{
	int i;
	sv_t *sv = (sv_t *)arg;

	down(&monitor);
	for(i = 0; i < 3; i++) {
		printk("sv_test_2_s: waking one thread (should be %d.)\n", i);
		sv_signal(sv);
		down(&sem);
	}

	printk("sv_test_3_s: waking remaining threads with broadcast.\n");
	sv_broadcast(sv);
	for(; i < 10; i++)
		down(&sem);

	printk("sv_test_3_s: sending talkback.\n");
	up(&talkback);

	printk("sv_test_3_s: exiting.\n");
	up(&monitor);
	return 0;
}


static void big_test(sv_t *sv) 
{
	int i;

	count = 0;

	for(i = 0; i < 3; i++) {
		printk("big_test: spawning thread %d.\n", i);
		kernel_thread(sv_test_2_w, sv, 0);
		down(&talkback);
	}

	printk("big_test: spawning first wake-up thread.\n");
	kernel_thread(sv_test_2_s_1, sv, 0);

	down(&talkback);
	printk("big_test: talkback happened.\n");


	for(i = 3; i < 13; i++) {
		printk("big_test: spawning thread %d.\n", i);
		kernel_thread(sv_test_2_w, sv, 0);
		down(&talkback);
	}

	printk("big_test: spawning wake-up thread.\n");
	kernel_thread(sv_test_2_s, sv, 0);

	down(&talkback);
}

sv_t int_test_sv;
spinlock_t int_test_spin = SPIN_LOCK_UNLOCKED;
int int_test_ready;
static int irqtestcount;

static int interrupt_test_worker(void *unused) 
{
	int id = ++irqtestcount;
	int it = 0;
			unsigned long flags, flags2;

	printk("ITW: thread %d started.\n", id);

	while(1) {
		local_save_flags(flags2);
		if(jiffies % 3) {
			printk("ITW %2d %5d: irqsaving          (%lx)\n", id, it, flags2);
			spin_lock_irqsave(&int_test_spin, flags);
		} else {
			printk("ITW %2d %5d: spin_lock_irqing   (%lx)\n", id, it, flags2);
			spin_lock_irq(&int_test_spin);
		}

		local_save_flags(flags2);
		printk("ITW %2d %5d: locked, sv_waiting (%lx).\n", id, it, flags2);
		sv_wait(&int_test_sv, 0, 0);

		local_save_flags(flags2);
		printk("ITW %2d %5d: wait finished      (%lx), pausing\n", id, it, flags2);
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(jiffies & 0xf);
		if(current->state != TASK_RUNNING)
		  printk("ITW:  current->state isn't RUNNING after schedule!\n");
		it++;
	}
}

static void interrupt_test(void) 
{
	int i;

	printk("interrupt_test: initing sv.\n");
	sv_init(&int_test_sv, &int_test_spin, SV_MON_SPIN | SV_INTS);

	for(i = 0; i < SV_INTERRUPT_TEST_WORKERS; i++) {
		printk("interrupt_test: starting test thread %d.\n", i);
		kernel_thread(interrupt_test_worker, 0, 0);
	}
	printk("interrupt_test: done with init part.\n");
	int_test_ready = 1;
}

int sv_test(void) 
{
	spinlock_t s = SPIN_LOCK_UNLOCKED;

	sv_init(&sv, &s, SV_MON_SPIN);
	printk("sv_test: starting sv_test_1_w.\n");
	kernel_thread(sv_test_1_w, &s, 0);
	printk("sv_test: starting sv_test_1_s.\n");
	kernel_thread(sv_test_1_s, &s, 0);

	printk("sv_test: waiting for talkback.\n");
	down(&talkback); down(&talkback);
	printk("sv_test: talkback happened, sv_destroying.\n");
	sv_destroy(&sv);

	count = 0;

	printk("sv_test: beginning big_test on sv.\n");

	sv_init(&sv, &monitor, SV_MON_SEMA);
	big_test(&sv);
	sv_destroy(&sv);

	printk("sv_test: beginning big_test on sv_filo.\n");
	sv_init(&sv_filo, &monitor, SV_MON_SEMA | SV_ORDER_FILO);
	big_test(&sv_filo);
	sv_destroy(&sv_filo);

	interrupt_test();

	printk("sv_test: done.\n");
	return 0;
}

__initcall(sv_test);

#endif /* RUN_SV_TEST */