PageRenderTime 47ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

Plain Text | 134 lines | 85 code | 49 blank | 0 comment | 0 complexity | 43cf696405b14d1cdc679418c40c362b MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. =====================================
  3. =====================================
  4. On the FR-V CPUs, there is only one atomic Read-Modify-Write operation: the SWAP/SWAPI
  5. instruction. Unfortunately, this alone can't be used to implement the following operations:
  6. (*) Atomic add to memory
  7. (*) Atomic subtract from memory
  8. (*) Atomic bit modification (set, clear or invert)
  9. (*) Atomic compare and exchange
  10. On such CPUs, the standard way of emulating such operations in uniprocessor mode is to disable
  11. interrupts, but on the FR-V CPUs, modifying the PSR takes a lot of clock cycles, and it has to be
  12. done twice. This means the CPU runs for a relatively long time with interrupts disabled,
  13. potentially having a great effect on interrupt latency.
  14. =============
  16. =============
  17. To get around this, the following algorithm has been implemented. It operates in a way similar to
  18. the LL/SC instruction pairs supported on a number of platforms.
  19. (*) The CCCR.CC3 register is reserved within the kernel to act as an atomic modify abort flag.
  20. (*) In the exception prologues run on kernel->kernel entry, CCCR.CC3 is set to 0 (Undefined
  21. state).
  22. (*) All atomic operations can then be broken down into the following algorithm:
  23. (1) Set ICC3.Z to true and set CC3 to True (ORCC/CKEQ/ORCR).
  24. (2) Load the value currently in the memory to be modified into a register.
  25. (3) Make changes to the value.
  26. (4) If CC3 is still True, simultaneously and atomically (by VLIW packing):
  27. (a) Store the modified value back to memory.
  28. (b) Set ICC3.Z to false (CORCC on GR29 is sufficient for this - GR29 holds the current
  29. task pointer in the kernel, and so is guaranteed to be non-zero).
  30. (5) If ICC3.Z is still true, go back to step (1).
  31. This works in a non-SMP environment because any interrupt or other exception that happens between
  32. steps (1) and (4) will set CC3 to the Undefined, thus aborting the store in (4a), and causing the
  33. condition in ICC3 to remain with the Z flag set, thus causing step (5) to loop back to step (1).
  34. This algorithm suffers from two problems:
  35. (1) The condition CCCR.CC3 is cleared unconditionally by an exception, irrespective of whether or
  36. not any changes were made to the target memory location during that exception.
  37. (2) The branch from step (5) back to step (1) may have to happen more than once until the store
  38. manages to take place. In theory, this loop could cycle forever because there are too many
  39. interrupts coming in, but it's unlikely.
  40. =======
  42. =======
  43. Taking an example from include/asm-frv/atomic.h:
  44. static inline int atomic_add_return(int i, atomic_t *v)
  45. {
  46. unsigned long val;
  47. asm("0: \n"
  48. It starts by setting ICC3.Z to true for later use, and also transforming that into CC3 being in the
  49. True state.
  50. " orcc gr0,gr0,gr0,icc3 \n" <-- (1)
  51. " ckeq icc3,cc7 \n" <-- (1)
  52. Then it does the load. Note that the final phase of step (1) is done at the same time as the
  53. load. The VLIW packing ensures they are done simultaneously. The ".p" on the load must not be
  54. removed without swapping the order of these two instructions.
  55. " ld.p %M0,%1 \n" <-- (2)
  56. " orcr cc7,cc7,cc3 \n" <-- (1)
  57. Then the proposed modification is generated. Note that the old value can be retained if required
  58. (such as in test_and_set_bit()).
  59. " add%I2 %1,%2,%1 \n" <-- (3)
  60. Then it attempts to store the value back, contingent on no exception having cleared CC3 since it
  61. was set to True.
  62. " cst.p %1,%M0 ,cc3,#1 \n" <-- (4a)
  63. It simultaneously records the success or failure of the store in ICC3.Z.
  64. " corcc gr29,gr29,gr0 ,cc3,#1 \n" <-- (4b)
  65. Such that the branch can then be taken if the operation was aborted.
  66. " beq icc3,#0,0b \n" <-- (5)
  67. : "+U"(v->counter), "=&r"(val)
  68. : "NPr"(i)
  69. : "memory", "cc7", "cc3", "icc3"
  70. );
  71. return val;
  72. }
  73. =============
  75. =============
  76. The atomic ops implementation can be made inline or out-of-line by changing the
  77. CONFIG_FRV_OUTOFLINE_ATOMIC_OPS configuration variable. Making it out-of-line has a number of
  78. advantages:
  79. - The resulting kernel image may be smaller
  80. - Debugging is easier as atomic ops can just be stepped over and they can be breakpointed
  81. Keeping it inline also has a number of advantages:
  82. - The resulting kernel may be Faster
  83. - no out-of-line function calls need to be made
  84. - the compiler doesn't have half its registers clobbered by making a call
  85. The out-of-line implementations live in arch/frv/lib/atomic-ops.S.