locks.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifdef CONFIG_PPC_SPLPAR
  43. else
  44. plpar_hcall_norets(H_CONFER,
  45. get_hard_smp_processor_id(holder_cpu), yield_count);
  46. #endif
  47. }
  48. /*
  49. * Waiting for a read lock or a write lock on a rwlock...
  50. * This turns out to be the same for read and write locks, since
  51. * we only know the holder if it is write-locked.
  52. */
  53. void __rw_yield(raw_rwlock_t *rw)
  54. {
  55. int lock_value;
  56. unsigned int holder_cpu, yield_count;
  57. lock_value = rw->lock;
  58. if (lock_value >= 0)
  59. return; /* no write lock at present */
  60. holder_cpu = lock_value & 0xffff;
  61. BUG_ON(holder_cpu >= NR_CPUS);
  62. yield_count = lppaca[holder_cpu].yield_count;
  63. if ((yield_count & 1) == 0)
  64. return; /* virtual cpu is currently running */
  65. rmb();
  66. if (rw->lock != lock_value)
  67. return; /* something has changed */
  68. if (firmware_has_feature(FW_FEATURE_ISERIES))
  69. HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
  70. ((u64)holder_cpu << 32) | yield_count);
  71. #ifdef CONFIG_PPC_SPLPAR
  72. else
  73. plpar_hcall_norets(H_CONFER,
  74. get_hard_smp_processor_id(holder_cpu), yield_count);
  75. #endif
  76. }
  77. #endif
  78. void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  79. {
  80. while (lock->slock) {
  81. HMT_low();
  82. if (SHARED_PROCESSOR)
  83. __spin_yield(lock);
  84. }
  85. HMT_medium();
  86. }
  87. EXPORT_SYMBOL(__raw_spin_unlock_wait);