cpu.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-09-15 Bernard first version
  9. * 2018-11-22 Jesven add rt_hw_cpu_id()
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <board.h>
  14. #ifdef RT_USING_SMP
  15. void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock)
  16. {
  17. lock->slock = 0;
  18. }
  19. void rt_hw_spin_lock(rt_hw_spinlock_t *lock)
  20. {
  21. unsigned long tmp;
  22. unsigned long newval;
  23. rt_hw_spinlock_t lockval;
  24. __asm__ __volatile__(
  25. "pld [%0]"
  26. ::"r"(&lock->slock)
  27. );
  28. __asm__ __volatile__(
  29. "1: ldrex %0, [%3]\n"
  30. " add %1, %0, %4\n"
  31. " strex %2, %1, [%3]\n"
  32. " teq %2, #0\n"
  33. " bne 1b"
  34. : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
  35. : "r" (&lock->slock), "I" (1 << 16)
  36. : "cc");
  37. while (lockval.tickets.next != lockval.tickets.owner) {
  38. __asm__ __volatile__("wfe":::"memory");
  39. lockval.tickets.owner = *(volatile unsigned short *)(&lock->tickets.owner);
  40. }
  41. __asm__ volatile ("dmb":::"memory");
  42. }
  43. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock)
  44. {
  45. __asm__ volatile ("dmb":::"memory");
  46. lock->tickets.owner++;
  47. __asm__ volatile ("dsb ishst\nsev":::"memory");
  48. }
  49. #endif /*RT_USING_SMP*/
  50. /**
  51. * @addtogroup ARM CPU
  52. */
  53. /*@{*/
  54. /** shutdown CPU */
  55. RT_WEAK void rt_hw_cpu_shutdown()
  56. {
  57. rt_uint32_t level;
  58. rt_kprintf("shutdown...\n");
  59. level = rt_hw_interrupt_disable();
  60. while (level)
  61. {
  62. RT_ASSERT(0);
  63. }
  64. }
  65. #ifdef RT_USING_CPU_FFS
  66. /**
  67. * This function finds the first bit set (beginning with the least significant bit)
  68. * in value and return the index of that bit.
  69. *
  70. * Bits are numbered starting at 1 (the least significant bit). A return value of
  71. * zero from any of these functions means that the argument was zero.
  72. *
  73. * @return return the index of the first bit set. If value is 0, then this function
  74. * shall return 0.
  75. */
  76. int __rt_ffs(int value)
  77. {
  78. return __builtin_ffs(value);
  79. }
  80. #endif
  81. /*@}*/