cpuport.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * 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 <lwp_arch.h>
  17. /**
  18. * @brief from thread used interrupt context switch
  19. *
  20. */
  21. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  22. /**
  23. * @brief to thread used interrupt context switch
  24. *
  25. */
  26. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  27. /**
  28. * @brief flag to indicate context switch in interrupt or not
  29. *
  30. */
  31. volatile rt_ubase_t rt_thread_switch_interrupt_flag = 0;
  32. /**
  33. * This function will initialize thread stack
  34. *
  35. * @param tentry the entry of thread
  36. * @param parameter the parameter of entry
  37. * @param stack_addr the beginning stack address
  38. * @param texit the function will be called when thread exit
  39. *
  40. * @return stack address
  41. */
  42. rt_uint8_t *rt_hw_stack_init(void *tentry,
  43. void *parameter,
  44. rt_uint8_t *stack_addr,
  45. void *texit)
  46. {
  47. struct rt_hw_stack_frame *frame;
  48. rt_uint8_t *stk;
  49. int i;
  50. extern int __global_pointer$;
  51. stk = stack_addr + sizeof(rt_ubase_t);
  52. stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_ubase_t)stk, REGBYTES);
  53. stk -= sizeof(struct rt_hw_stack_frame);
  54. frame = (struct rt_hw_stack_frame *)stk;
  55. for (i = 0; i < sizeof(struct rt_hw_stack_frame) / sizeof(rt_ubase_t); i++)
  56. {
  57. ((rt_ubase_t *)frame)[i] = 0xdeadbeef;
  58. }
  59. frame->ra = (rt_ubase_t)texit;
  60. frame->gp = (rt_ubase_t)&__global_pointer$;
  61. frame->a0 = (rt_ubase_t)parameter;
  62. frame->epc = (rt_ubase_t)tentry;
  63. frame->user_sp_exc_stack = (rt_ubase_t)(((rt_ubase_t)stk) + sizeof(struct rt_hw_stack_frame));
  64. /* force to supervisor mode(SPP=1) and set SPIE and SUM to 1 */
  65. #ifdef ENABLE_FPU
  66. frame->sstatus = 0x00046120; /* enable FPU */
  67. #else
  68. frame->sstatus = 0x00040120;
  69. #endif
  70. return stk;
  71. }
  72. /*
  73. * #ifdef RT_USING_SMP
  74. * void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  75. * #else
  76. * void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
  77. * #endif
  78. */
  79. #ifndef RT_USING_SMP
  80. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
  81. {
  82. if (rt_thread_switch_interrupt_flag == 0)
  83. rt_interrupt_from_thread = from;
  84. rt_interrupt_to_thread = to;
  85. rt_thread_switch_interrupt_flag = 1;
  86. return ;
  87. }
  88. #endif /* end of RT_USING_SMP */
  89. /** shutdown CPU */
  90. void rt_hw_cpu_shutdown()
  91. {
  92. rt_uint32_t level;
  93. rt_kprintf("shutdown...\n");
  94. level = rt_hw_interrupt_disable();
  95. while (level)
  96. {
  97. RT_ASSERT(0);
  98. }
  99. }
  100. int rt_hw_cpu_id(void)
  101. {
  102. return 0; /* d1 has one core */
  103. }
  104. void rt_hw_set_process_id(int pid)
  105. {
  106. //TODO
  107. }