entry_user.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdlib.h>
  16. #include "atomic.h"
  17. #include "clint.h"
  18. #include "dmac.h"
  19. #include "entry.h"
  20. #include "fpioa.h"
  21. #include "platform.h"
  22. #include "plic.h"
  23. #include "syscalls.h"
  24. #include "sysctl.h"
  25. #include "syslog.h"
  26. #include "uart.h"
  27. extern volatile uint64_t g_wake_up[2];
  28. core_instance_t core1_instance;
  29. volatile char *const ram = (volatile char *)RAM_BASE_ADDR;
  30. extern char _heap_start[];
  31. extern char _heap_end[];
  32. void __attribute__((weak)) initialize_kendryte_ide_hook(void) {}
  33. void thread_entry(int core_id)
  34. {
  35. while(!atomic_read(&g_wake_up[core_id]))
  36. ;
  37. }
  38. void core_enable(int core_id)
  39. {
  40. clint_ipi_send(core_id);
  41. atomic_set(&g_wake_up[core_id], 1);
  42. }
  43. int register_core1(core_function func, void *ctx)
  44. {
  45. if(func == NULL)
  46. return -1;
  47. core1_instance.callback = func;
  48. core1_instance.ctx = ctx;
  49. core_enable(1);
  50. return 0;
  51. }
  52. int __attribute__((weak)) os_entry(int core_id, int number_of_cores, int (*user_main)(int, char **))
  53. {
  54. /* Call main if there is no OS */
  55. return user_main(0, 0);
  56. }
  57. void _init_bsp(int core_id, int number_of_cores)
  58. {
  59. extern int main(int argc, char *argv[]);
  60. extern void __libc_init_array(void);
  61. extern void __libc_fini_array(void);
  62. if(core_id == 0)
  63. {
  64. /* Initialize bss data to 0 */
  65. init_bss();
  66. /* Init UART */
  67. fpioa_set_function(4, FUNC_UART3_RX);
  68. fpioa_set_function(5, FUNC_UART3_TX);
  69. uart_debug_init(UART_DEVICE_3);
  70. dmac_init();
  71. /* Init FPIOA */
  72. fpioa_init();
  73. /* Register finalization function */
  74. atexit(__libc_fini_array);
  75. /* Init libc array for C++ */
  76. __libc_init_array();
  77. /* Get reset status */
  78. sysctl_get_reset_status();
  79. /* Init plic */
  80. plic_init();
  81. /* Enable global interrupt */
  82. sysctl_enable_irq();
  83. /* Hook entry for kendryte IDE */
  84. initialize_kendryte_ide_hook();
  85. }
  86. int ret = 0;
  87. if(core_id == 0)
  88. {
  89. core1_instance.callback = NULL;
  90. core1_instance.ctx = NULL;
  91. ret = os_entry(core_id, number_of_cores, main);
  92. } else
  93. {
  94. plic_init();
  95. sysctl_enable_irq();
  96. thread_entry(core_id);
  97. if(core1_instance.callback == NULL)
  98. asm volatile("wfi");
  99. else
  100. ret = core1_instance.callback(core1_instance.ctx);
  101. }
  102. exit(ret);
  103. }
  104. int pthread_setcancelstate(int __state, int *__oldstate)
  105. {
  106. return 0;
  107. }