cpuport.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * 2023-10-25 Shell Move ffs to cpuport, add general implementation
  9. * by inline assembly
  10. */
  11. #ifndef CPUPORT_H__
  12. #define CPUPORT_H__
  13. #include <armv8.h>
  14. #include <rtdef.h>
  15. #ifdef RT_USING_SMP
  16. typedef struct {
  17. volatile unsigned int lock;
  18. } rt_hw_spinlock_t;
  19. #endif
  20. #define rt_hw_barrier(cmd, ...) \
  21. __asm__ volatile (RT_STRINGIFY(cmd) " "RT_STRINGIFY(__VA_ARGS__):::"memory")
  22. #define rt_hw_isb() rt_hw_barrier(isb)
  23. #define rt_hw_dmb() rt_hw_barrier(dmb, ish)
  24. #define rt_hw_wmb() rt_hw_barrier(dmb, ishst)
  25. #define rt_hw_rmb() rt_hw_barrier(dmb, ishld)
  26. #define rt_hw_dsb() rt_hw_barrier(dsb, ish)
  27. #define rt_hw_wfi() rt_hw_barrier(wfi)
  28. #define rt_hw_wfe() rt_hw_barrier(wfe)
  29. #define rt_hw_sev() rt_hw_barrier(sev)
  30. #define rt_hw_cpu_relax() rt_hw_barrier(yield)
  31. void _thread_start(void);
  32. #ifdef RT_USING_CPU_FFS
  33. /**
  34. * This function finds the first bit set (beginning with the least significant bit)
  35. * in value and return the index of that bit.
  36. *
  37. * Bits are numbered starting at 1 (the least significant bit). A return value of
  38. * zero from any of these functions means that the argument was zero.
  39. *
  40. * @return return the index of the first bit set. If value is 0, then this function
  41. * shall return 0.
  42. */
  43. rt_inline int __rt_ffs(int value)
  44. {
  45. #ifdef __GNUC__
  46. return __builtin_ffs(value);
  47. #else
  48. __asm__ volatile (
  49. "rbit w1, %w0\n"
  50. "cmp %w0, 0\n"
  51. "clz w1, w1\n"
  52. "csinc %w0, wzr, w1, eq\n"
  53. : "=r"(value)
  54. : "0"(value)
  55. );
  56. return value;
  57. #endif
  58. }
  59. #endif /* RT_USING_CPU_FFS */
  60. #endif /*CPUPORT_H__*/