blob: 077bed7dc52b3726ae4ebd7ecb227832fd52de95 [file] [log] [blame]
Paul Mackerras40ef8cb2005-10-10 22:50:37 +10001/*
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
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100015#include <linux/kernel.h>
16#include <linux/spinlock.h>
17#include <linux/module.h>
18#include <linux/stringify.h>
Paul Mackerras734d6522005-10-31 13:57:01 +110019#include <linux/smp.h>
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100020
21/* waiting for a spinlock... */
22#if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
23#include <asm/hvcall.h>
Kelly Daly1da44032005-11-01 16:59:20 +110024#include <asm/iseries/hv_call.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110025#include <asm/smp.h>
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100026
27void __spin_yield(raw_spinlock_t *lock)
28{
29 unsigned int lock_value, holder_cpu, yield_count;
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100030
31 lock_value = lock->slock;
32 if (lock_value == 0)
33 return;
34 holder_cpu = lock_value & 0xffff;
35 BUG_ON(holder_cpu >= NR_CPUS);
David Gibson3356bb92006-01-13 10:26:42 +110036 yield_count = lppaca[holder_cpu].yield_count;
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100037 if ((yield_count & 1) == 0)
38 return; /* virtual cpu is currently running */
39 rmb();
40 if (lock->slock != lock_value)
41 return; /* something has changed */
42#ifdef CONFIG_PPC_ISERIES
43 HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
44 ((u64)holder_cpu << 32) | yield_count);
45#else
46 plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu),
47 yield_count);
48#endif
49}
50
51/*
52 * Waiting for a read lock or a write lock on a rwlock...
53 * This turns out to be the same for read and write locks, since
54 * we only know the holder if it is write-locked.
55 */
56void __rw_yield(raw_rwlock_t *rw)
57{
58 int lock_value;
59 unsigned int holder_cpu, yield_count;
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100060
61 lock_value = rw->lock;
62 if (lock_value >= 0)
63 return; /* no write lock at present */
64 holder_cpu = lock_value & 0xffff;
65 BUG_ON(holder_cpu >= NR_CPUS);
David Gibson3356bb92006-01-13 10:26:42 +110066 yield_count = lppaca[holder_cpu].yield_count;
Paul Mackerras40ef8cb2005-10-10 22:50:37 +100067 if ((yield_count & 1) == 0)
68 return; /* virtual cpu is currently running */
69 rmb();
70 if (rw->lock != lock_value)
71 return; /* something has changed */
72#ifdef CONFIG_PPC_ISERIES
73 HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
74 ((u64)holder_cpu << 32) | yield_count);
75#else
76 plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu),
77 yield_count);
78#endif
79}
80#endif
81
82void __raw_spin_unlock_wait(raw_spinlock_t *lock)
83{
84 while (lock->slock) {
85 HMT_low();
86 if (SHARED_PROCESSOR)
87 __spin_yield(lock);
88 }
89 HMT_medium();
90}
91
92EXPORT_SYMBOL(__raw_spin_unlock_wait);