main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files
  8. * (the "Software"), to deal in the Software without restriction,
  9. * including without limitation the rights to use, copy, modify, merge,
  10. * publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <compiler.h>
  26. #include <debug.h>
  27. #include <string.h>
  28. #include <app.h>
  29. #include <arch.h>
  30. #include <platform.h>
  31. #include <target.h>
  32. #include <lib/heap.h>
  33. #include <kernel/thread.h>
  34. #include <kernel/timer.h>
  35. #include <kernel/dpc.h>
  36. #include <platform/mt_gpt.h>
  37. #include <iothread.h>
  38. #include <version.h>
  39. #ifdef MTK_LK_IRRX_SUPPORT
  40. #include <platform/mtk_ir_lk_core.h>
  41. #endif
  42. extern void (*__ctor_list[])(void);
  43. extern void (*__ctor_end[])(void);
  44. extern int __bss_start;
  45. extern int _end;
  46. extern void mboot_allocate_lk_scratch_from_mblock(void);
  47. /* boot_time is calculated form kmain to kernel jump */
  48. volatile unsigned int boot_time = 0;
  49. static int bootstrap2(void *arg);
  50. #if (ENABLE_NANDWRITE)
  51. void bootstrap_nandwrite(void);
  52. #endif
  53. static void print_stack_of_current_thread(void)
  54. {
  55. extern thread_t *current_thread;
  56. if (current_thread)
  57. {
  58. char *stack_limit = current_thread->stack;
  59. char *stack_bottom = stack_limit + current_thread->stack_size;
  60. dprintf(CRITICAL, "thread: %s, bottom(stack) = %p, limit(stack) = %p\n",
  61. current_thread->name, stack_bottom, stack_limit);
  62. }
  63. }
  64. static void call_constructors(void)
  65. {
  66. void (**ctor)(void);
  67. for (ctor = __ctor_list; ctor != __ctor_end; ctor++)
  68. (*ctor)();
  69. }
  70. /* called from crt0.S */
  71. void kmain(void) __NO_RETURN __EXTERNALLY_VISIBLE;
  72. void kmain(void)
  73. {
  74. #if !defined(MACH_FPGA) && !defined(SB_LK_BRINGUP)
  75. boot_time = get_timer(0);
  76. #endif
  77. // get us into some sort of thread context
  78. thread_init_early();
  79. // early arch stuff
  80. arch_early_init();
  81. // do any super early platform initialization
  82. platform_early_init();
  83. #if defined(MACH_FPGA) || defined(SB_LK_BRINGUP)
  84. boot_time = get_timer(0);
  85. #endif
  86. // do any super early target initialization
  87. target_early_init();
  88. dprintf(INFO, "welcome to lk\n\n");
  89. dprintf(INFO, "LK_VER_TAG = %s\n", LK_VER_TAG);
  90. // deal with any static constructors
  91. dprintf(SPEW, "calling constructors\n");
  92. call_constructors();
  93. // bring up the kernel heap
  94. dprintf(SPEW, "initializing heap\n");
  95. heap_init();
  96. // initialize the threading system
  97. dprintf(SPEW, "initializing threads\n");
  98. thread_init();
  99. // initialize the dpc system
  100. dprintf(SPEW, "initializing dpc\n");
  101. dpc_init();
  102. // initialize kernel timers
  103. dprintf(SPEW, "initializing timers\n");
  104. timer_init();
  105. #ifdef MTK_LK_IRRX_SUPPORT
  106. mtk_ir_init(0);
  107. #endif
  108. #if (!ENABLE_NANDWRITE)
  109. // create a thread to complete system initialization
  110. dprintf(SPEW, "creating bootstrap completion thread\n");
  111. thread_t *thread_bs2 = thread_create("bootstrap2", &bootstrap2, NULL,
  112. DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
  113. if (thread_bs2)
  114. thread_resume(thread_bs2);
  115. else {
  116. dprintf(CRITICAL, "Error: Cannot create bootstrap2 thread!\n");
  117. assert(0);
  118. }
  119. thread_t *thread_io = thread_create("iothread", &iothread, NULL,
  120. IO_THREAD_PRIORITY, DEFAULT_STACK_SIZE);
  121. if (thread_io)
  122. thread_resume(thread_io);
  123. else {
  124. dprintf(CRITICAL, "Error: Cannot create I/O thread!\n");
  125. assert(0);
  126. }
  127. // enable interrupts
  128. exit_critical_section();
  129. // become the idle thread
  130. thread_become_idle();
  131. #else
  132. bootstrap_nandwrite();
  133. #endif
  134. }
  135. static int bootstrap2(void *arg)
  136. {
  137. dprintf(SPEW, "top of bootstrap2()\n");
  138. print_stack_of_current_thread();
  139. arch_init();
  140. // XXX put this somewhere else
  141. #if WITH_LIB_BIO
  142. bio_init();
  143. #endif
  144. #if WITH_LIB_FS
  145. fs_init();
  146. #endif
  147. // Allocate LK memory from mb, free before jump to kernel
  148. mboot_allocate_lk_scratch_from_mblock();
  149. // initialize the rest of the platform
  150. dprintf(SPEW, "initializing platform\n");
  151. platform_init();
  152. // initialize the target
  153. dprintf(SPEW, "initializing target\n");
  154. target_init();
  155. dprintf(SPEW, "calling apps_init()\n");
  156. apps_init();
  157. return 0;
  158. }
  159. #if (ENABLE_NANDWRITE)
  160. void bootstrap_nandwrite(void)
  161. {
  162. dprintf(SPEW, "top of bootstrap2()\n");
  163. arch_init();
  164. // initialize the rest of the platform
  165. dprintf(SPEW, "initializing platform\n");
  166. platform_init();
  167. // initialize the target
  168. dprintf(SPEW, "initializing target\n");
  169. target_init();
  170. dprintf(SPEW, "calling nandwrite_init()\n");
  171. nandwrite_init();
  172. return 0;
  173. }
  174. #endif