spinlock_common.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. * This file is included into spinlock_32.c or _64.c.
  14. */
  15. /*
  16. * The mfspr in __spinlock_relax() is 5 or 6 cycles plus 2 for loop
  17. * overhead.
  18. */
  19. #ifdef __tilegx__
  20. #define CYCLES_PER_RELAX_LOOP 7
  21. #else
  22. #define CYCLES_PER_RELAX_LOOP 8
  23. #endif
  24. /*
  25. * Idle the core for CYCLES_PER_RELAX_LOOP * iterations cycles.
  26. */
  27. static inline void
  28. relax(int iterations)
  29. {
  30. for (/*above*/; iterations > 0; iterations--)
  31. __insn_mfspr(SPR_PASS);
  32. barrier();
  33. }
  34. /* Perform bounded exponential backoff.*/
  35. static void delay_backoff(int iterations)
  36. {
  37. u32 exponent, loops;
  38. /*
  39. * 2^exponent is how many times we go around the loop,
  40. * which takes 8 cycles. We want to start with a 16- to 31-cycle
  41. * loop, so we need to go around minimum 2 = 2^1 times, so we
  42. * bias the original value up by 1.
  43. */
  44. exponent = iterations + 1;
  45. /*
  46. * Don't allow exponent to exceed 7, so we have 128 loops,
  47. * or 1,024 (to 2,047) cycles, as our maximum.
  48. */
  49. if (exponent > 8)
  50. exponent = 8;
  51. loops = 1 << exponent;
  52. /* Add a randomness factor so two cpus never get in lock step. */
  53. loops += __insn_crc32_32(stack_pointer, get_cycles_low()) &
  54. (loops - 1);
  55. relax(loops);
  56. }