locks.c 2.3 KB

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