cpuport.c 2.8 KB

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