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