locks.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/HvCall.h>
  24. void __spin_yield(raw_spinlock_t *lock)
  25. {
  26. unsigned int lock_value, holder_cpu, yield_count;
  27. struct paca_struct *holder_paca;
  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. holder_paca = &paca[holder_cpu];
  34. yield_count = holder_paca->lppaca.yield_count;
  35. if ((yield_count & 1) == 0)
  36. return; /* virtual cpu is currently running */
  37. rmb();
  38. if (lock->slock != lock_value)
  39. return; /* something has changed */
  40. #ifdef CONFIG_PPC_ISERIES
  41. HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
  42. ((u64)holder_cpu << 32) | yield_count);
  43. #else
  44. plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu),
  45. 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. struct paca_struct *holder_paca;
  58. lock_value = rw->lock;
  59. if (lock_value >= 0)
  60. return; /* no write lock at present */
  61. holder_cpu = lock_value & 0xffff;
  62. BUG_ON(holder_cpu >= NR_CPUS);
  63. holder_paca = &paca[holder_cpu];
  64. yield_count = holder_paca->lppaca.yield_count;
  65. if ((yield_count & 1) == 0)
  66. return; /* virtual cpu is currently running */
  67. rmb();
  68. if (rw->lock != lock_value)
  69. return; /* something has changed */
  70. #ifdef CONFIG_PPC_ISERIES
  71. HvCall2(HvCallBaseYieldProcessor, HvCall_YieldToProc,
  72. ((u64)holder_cpu << 32) | yield_count);
  73. #else
  74. plpar_hcall_norets(H_CONFER, get_hard_smp_processor_id(holder_cpu),
  75. yield_count);
  76. #endif
  77. }
  78. #endif
  79. void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  80. {
  81. while (lock->slock) {
  82. HMT_low();
  83. if (SHARED_PROCESSOR)
  84. __spin_yield(lock);
  85. }
  86. HMT_medium();
  87. }
  88. EXPORT_SYMBOL(__raw_spin_unlock_wait);