locks.c 2.6 KB

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