locks.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <linux/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/module.h>
  18. #include <linux/stringify.h>
  19. #include <linux/smp.h>
  20. /* waiting for a spinlock... */
  21. #if defined(CONFIG_PPC_SPLPAR) || defined(CONFIG_PPC_ISERIES)
  22. #include <asm/hvcall.h>
  23. #include <asm/iseries/hv_call.h>
  24. #include <asm/smp.h>
  25. void __spin_yield(raw_spinlock_t *lock)
  26. {
  27. unsigned int lock_value, holder_cpu, yield_count;
  28. struct paca_struct *holder_paca;
  29. lock_value = lock->slock;
  30. if (lock_value == 0)
  31. return;
  32. holder_cpu = lock_value & 0xffff;
  33. BUG_ON(holder_cpu >= NR_CPUS);
  34. holder_paca = &paca[holder_cpu];
  35. yield_count = holder_paca->lppaca.yield_count;
  36. if ((yield_count & 1) == 0)
  37. return; /* virtual cpu is currently running */
  38. rmb();
  39. if (lock->slock != lock_value)
  40. return; /* something has changed */
  41. #ifdef CONFIG_PPC_ISERIES
  42. HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
  43. ((u64)holder_cpu << 32) | yield_count);
  44. #else
  45. plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu),
  46. yield_count);
  47. #endif
  48. }
  49. /*
  50. * Waiting for a read lock or a write lock on a rwlock...
  51. * This turns out to be the same for read and write locks, since
  52. * we only know the holder if it is write-locked.
  53. */
  54. void __rw_yield(raw_rwlock_t *rw)
  55. {
  56. int lock_value;
  57. unsigned int holder_cpu, yield_count;
  58. struct paca_struct *holder_paca;
  59. lock_value = rw->lock;
  60. if (lock_value >= 0)
  61. return; /* no write lock at present */
  62. holder_cpu = lock_value & 0xffff;
  63. BUG_ON(holder_cpu >= NR_CPUS);
  64. holder_paca = &paca[holder_cpu];
  65. yield_count = holder_paca->lppaca.yield_count;
  66. if ((yield_count & 1) == 0)
  67. return; /* virtual cpu is currently running */
  68. rmb();
  69. if (rw->lock != lock_value)
  70. return; /* something has changed */
  71. #ifdef CONFIG_PPC_ISERIES
  72. HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
  73. ((u64)holder_cpu << 32) | yield_count);
  74. #else
  75. plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu),
  76. yield_count);
  77. #endif
  78. }
  79. #endif
  80. void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  81. {
  82. while (lock->slock) {
  83. HMT_low();
  84. if (SHARED_PROCESSOR)
  85. __spin_yield(lock);
  86. }
  87. HMT_medium();
  88. }
  89. EXPORT_SYMBOL(__raw_spin_unlock_wait);