cpuport.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/10/28 Bernard The unify RISC-V porting code.
  9. * 2021-02-11 lizhirui add gp support
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "cpuport.h"
  14. #include "stack.h"
  15. #include <sbi.h>
  16. #include <encoding.h>
  17. #include "regtypes.h"
  18. #define K_SSTATUS_DEFAULT (SSTATUS_SPP | SSTATUS_SPIE | SSTATUS_SUM | SSTATUS_FS)
  19. /**
  20. * @brief from thread used interrupt context switch
  21. *
  22. */
  23. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  24. /**
  25. * @brief to thread used interrupt context switch
  26. *
  27. */
  28. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  29. /**
  30. * @brief flag to indicate context switch in interrupt or not
  31. *
  32. */
  33. volatile rt_ubase_t rt_thread_switch_interrupt_flag = 0;
  34. void *_rt_hw_stack_init(rt_uintreg_t *sp, rt_uintreg_t ra, rt_uintreg_t sstatus)
  35. {
  36. (*--sp) = 0; /* tp */
  37. (*--sp) = ra; /* ra */
  38. (*--sp) = 0; /* s0(fp) */
  39. (*--sp) = 0; /* s1 */
  40. (*--sp) = 0; /* s2 */
  41. (*--sp) = 0; /* s3 */
  42. (*--sp) = 0; /* s4 */
  43. (*--sp) = 0; /* s5 */
  44. (*--sp) = 0; /* s6 */
  45. (*--sp) = 0; /* s7 */
  46. (*--sp) = 0; /* s8 */
  47. (*--sp) = 0; /* s9 */
  48. (*--sp) = 0; /* s10 */
  49. (*--sp) = 0; /* s11 */
  50. (*--sp) = sstatus; /* sstatus */
  51. --sp; /* align to 16bytes */
  52. return (void *)sp;
  53. }
  54. int rt_hw_cpu_id(void)
  55. {
  56. return 0;
  57. }
  58. /**
  59. * This function will initialize thread stack, we assuming
  60. * when scheduler restore this new thread, context will restore
  61. * an entry to user first application
  62. *
  63. * s0-s11, ra, sstatus, a0
  64. * @param tentry the entry of thread
  65. * @param parameter the parameter of entry
  66. * @param stack_addr the beginning stack address
  67. * @param texit the function will be called when thread exit
  68. *
  69. * @return stack address
  70. */
  71. rt_uint8_t *rt_hw_stack_init(void *tentry,
  72. void *parameter,
  73. rt_uint8_t *stack_addr,
  74. void *texit)
  75. {
  76. rt_uintreg_t *sp = (rt_uintreg_t *)stack_addr;
  77. // we use a strict alignment requirement for Q extension
  78. sp = (rt_uintreg_t *)RT_ALIGN_DOWN((rt_uintreg_t)sp, 16);
  79. (*--sp) = (rt_uintreg_t)tentry;
  80. (*--sp) = (rt_uintreg_t)parameter;
  81. (*--sp) = (rt_uintreg_t)texit;
  82. /* compatible to RESTORE_CONTEXT */
  83. extern void _rt_thread_entry(void);
  84. return (rt_uint8_t *)_rt_hw_stack_init(sp, (rt_uintreg_t)_rt_thread_entry, K_SSTATUS_DEFAULT);
  85. }
  86. /*
  87. * #ifdef RT_USING_SMP
  88. * void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  89. * #else
  90. * void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
  91. * #endif
  92. */
  93. #ifndef RT_USING_SMP
  94. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
  95. {
  96. if (rt_thread_switch_interrupt_flag == 0)
  97. rt_interrupt_from_thread = from;
  98. rt_interrupt_to_thread = to;
  99. rt_thread_switch_interrupt_flag = 1;
  100. return;
  101. }
  102. #endif /* end of RT_USING_SMP */
  103. /** shutdown CPU */
  104. void rt_hw_cpu_shutdown(void)
  105. {
  106. rt_uint32_t level;
  107. rt_kprintf("shutdown...\n");
  108. level = rt_hw_interrupt_disable();
  109. sbi_shutdown();
  110. while (1)
  111. ;
  112. }
  113. void rt_hw_set_process_id(int pid)
  114. {
  115. // TODO
  116. }