locks.c 2.5 KB

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