cpuport.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * 2021-11-19 JasonHu add fpu support
  11. */
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include "cpuport.h"
  15. #include "stack.h"
  16. #include <sbi.h>
  17. #include <encoding.h>
  18. #ifdef ARCH_RISCV_FPU
  19. #define K_SSTATUS_DEFAULT_BASE (SSTATUS_SPP | SSTATUS_SPIE | SSTATUS_SUM | SSTATUS_FS)
  20. #else
  21. #define K_SSTATUS_DEFAULT_BASE (SSTATUS_SPP | SSTATUS_SPIE | SSTATUS_SUM)
  22. #endif
  23. #ifdef ARCH_RISCV_VECTOR
  24. #define K_SSTATUS_DEFAULT (K_SSTATUS_DEFAULT_BASE | SSTATUS_VS)
  25. #else
  26. #define K_SSTATUS_DEFAULT K_SSTATUS_DEFAULT_BASE
  27. #endif
  28. #ifdef RT_USING_SMART
  29. #include <lwp_arch.h>
  30. #endif
  31. /**
  32. * @brief from thread used interrupt context switch
  33. *
  34. */
  35. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  36. /**
  37. * @brief to thread used interrupt context switch
  38. *
  39. */
  40. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  41. /**
  42. * @brief flag to indicate context switch in interrupt or not
  43. *
  44. */
  45. volatile rt_ubase_t rt_thread_switch_interrupt_flag = 0;
  46. void *_rt_hw_stack_init(rt_ubase_t *sp, rt_ubase_t ra, rt_ubase_t sstatus)
  47. {
  48. rt_hw_switch_frame_t frame = (rt_hw_switch_frame_t)
  49. ((rt_ubase_t)sp - sizeof(struct rt_hw_switch_frame));
  50. rt_memset(frame, 0, sizeof(struct rt_hw_switch_frame));
  51. frame->regs[RT_HW_SWITCH_CONTEXT_RA] = ra;
  52. frame->regs[RT_HW_SWITCH_CONTEXT_SSTATUS] = sstatus;
  53. return (void *)frame;
  54. }
  55. int rt_hw_cpu_id(void)
  56. {
  57. return 0;
  58. }
  59. /**
  60. * This function will initialize thread stack, we assuming
  61. * when scheduler restore this new thread, context will restore
  62. * an entry to user first application
  63. *
  64. * s0-s11, ra, sstatus, a0
  65. * @param tentry the entry of thread
  66. * @param parameter the parameter of entry
  67. * @param stack_addr the beginning stack address
  68. * @param texit the function will be called when thread exit
  69. *
  70. * @return stack address
  71. */
  72. rt_uint8_t *rt_hw_stack_init(void *tentry,
  73. void *parameter,
  74. rt_uint8_t *stack_addr,
  75. void *texit)
  76. {
  77. rt_ubase_t *sp = (rt_ubase_t *)stack_addr;
  78. // we use a strict alignment requirement for Q extension
  79. sp = (rt_ubase_t *)RT_ALIGN_DOWN((rt_ubase_t)sp, 16);
  80. (*--sp) = (rt_ubase_t)tentry;
  81. (*--sp) = (rt_ubase_t)parameter;
  82. (*--sp) = (rt_ubase_t)texit;
  83. --sp; /* alignment */
  84. /* compatible to RESTORE_CONTEXT */
  85. extern void _rt_thread_entry(void);
  86. return (rt_uint8_t *)_rt_hw_stack_init(sp, (rt_ubase_t)_rt_thread_entry, K_SSTATUS_DEFAULT);
  87. }
  88. /*
  89. * #ifdef RT_USING_SMP
  90. * void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  91. * #else
  92. * void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
  93. * #endif
  94. */
  95. #ifndef RT_USING_SMP
  96. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
  97. {
  98. if (rt_thread_switch_interrupt_flag == 0)
  99. rt_interrupt_from_thread = from;
  100. rt_interrupt_to_thread = to;
  101. rt_thread_switch_interrupt_flag = 1;
  102. return;
  103. }
  104. #endif /* end of RT_USING_SMP */
  105. /** shutdown CPU */
  106. void rt_hw_cpu_shutdown(void)
  107. {
  108. rt_uint32_t level;
  109. rt_kprintf("shutdown...\n");
  110. level = rt_hw_interrupt_disable();
  111. sbi_shutdown();
  112. while (1)
  113. ;
  114. }
  115. void rt_hw_set_process_id(int pid)
  116. {
  117. // TODO
  118. }