xref: /linux/arch/powerpc/lib/locks.c (revision 858259cf7d1c443c836a2022b78cb281f0a9b95e)
1 /*
2  * Spin and read/write lock operations.
3  *
4  * Copyright (C) 2001-2004 Paul Mackerras <paulus@au.ibm.com>, IBM
5  * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
6  * Copyright (C) 2002 Dave Engebretsen <engebret@us.ibm.com>, IBM
7  *   Rework to support virtual processors
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version
12  * 2 of the License, or (at your option) any later version.
13  */
14 
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/spinlock.h>
18 #include <linux/module.h>
19 #include <linux/stringify.h>
20 #include <linux/smp.h>
21 
22 /* waiting for a spinlock... */
23 #if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
24 #include <asm/hvcall.h>
25 #include <asm/iseries/hv_call.h>
26 
27 void __spin_yield(raw_spinlock_t *lock)
28 {
29 	unsigned int lock_value, holder_cpu, yield_count;
30 	struct paca_struct *holder_paca;
31 
32 	lock_value = lock->slock;
33 	if (lock_value == 0)
34 		return;
35 	holder_cpu = lock_value & 0xffff;
36 	BUG_ON(holder_cpu >= NR_CPUS);
37 	holder_paca = &paca[holder_cpu];
38 	yield_count = holder_paca->lppaca.yield_count;
39 	if ((yield_count & 1) == 0)
40 		return;		/* virtual cpu is currently running */
41 	rmb();
42 	if (lock->slock != lock_value)
43 		return;		/* something has changed */
44 #ifdef CONFIG_PPC_ISERIES
45 	HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
46 		((u64)holder_cpu << 32) | yield_count);
47 #else
48 	plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu),
49 			   yield_count);
50 #endif
51 }
52 
53 /*
54  * Waiting for a read lock or a write lock on a rwlock...
55  * This turns out to be the same for read and write locks, since
56  * we only know the holder if it is write-locked.
57  */
58 void __rw_yield(raw_rwlock_t *rw)
59 {
60 	int lock_value;
61 	unsigned int holder_cpu, yield_count;
62 	struct paca_struct *holder_paca;
63 
64 	lock_value = rw->lock;
65 	if (lock_value >= 0)
66 		return;		/* no write lock at present */
67 	holder_cpu = lock_value & 0xffff;
68 	BUG_ON(holder_cpu >= NR_CPUS);
69 	holder_paca = &paca[holder_cpu];
70 	yield_count = holder_paca->lppaca.yield_count;
71 	if ((yield_count & 1) == 0)
72 		return;		/* virtual cpu is currently running */
73 	rmb();
74 	if (rw->lock != lock_value)
75 		return;		/* something has changed */
76 #ifdef CONFIG_PPC_ISERIES
77 	HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
78 		((u64)holder_cpu << 32) | yield_count);
79 #else
80 	plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu),
81 			   yield_count);
82 #endif
83 }
84 #endif
85 
86 void __raw_spin_unlock_wait(raw_spinlock_t *lock)
87 {
88 	while (lock->slock) {
89 		HMT_low();
90 		if (SHARED_PROCESSOR)
91 			__spin_yield(lock);
92 	}
93 	HMT_medium();
94 }
95 
96 EXPORT_SYMBOL(__raw_spin_unlock_wait);
97