head.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2012 Regents of the University of California
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <asm/thread_info.h>
  14. #include <asm/asm-offsets.h>
  15. #include <asm/asm.h>
  16. #include <linux/init.h>
  17. #include <linux/linkage.h>
  18. #include <asm/thread_info.h>
  19. #include <asm/page.h>
  20. #include <asm/csr.h>
  21. __INIT
  22. ENTRY(_start)
  23. /* Mask all interrupts */
  24. csrw sie, zero
  25. /* Load the global pointer */
  26. .option push
  27. .option norelax
  28. la gp, __global_pointer$
  29. .option pop
  30. /*
  31. * Disable FPU to detect illegal usage of
  32. * floating point in kernel space
  33. */
  34. li t0, SR_FS
  35. csrc sstatus, t0
  36. /* Pick one hart to run the main boot sequence */
  37. la a3, hart_lottery
  38. li a2, 1
  39. amoadd.w a3, a2, (a3)
  40. bnez a3, .Lsecondary_start
  41. /* Clear BSS for flat non-ELF images */
  42. la a3, __bss_start
  43. la a4, __bss_stop
  44. ble a4, a3, clear_bss_done
  45. clear_bss:
  46. REG_S zero, (a3)
  47. add a3, a3, RISCV_SZPTR
  48. blt a3, a4, clear_bss
  49. clear_bss_done:
  50. /* Save hart ID and DTB physical address */
  51. mv s0, a0
  52. mv s1, a1
  53. la a2, boot_cpu_hartid
  54. REG_S a0, (a2)
  55. /* Initialize page tables and relocate to virtual addresses */
  56. la sp, init_thread_union + THREAD_SIZE
  57. call setup_vm
  58. call relocate
  59. /* Restore C environment */
  60. la tp, init_task
  61. sw zero, TASK_TI_CPU(tp)
  62. la sp, init_thread_union
  63. li a0, ASM_THREAD_SIZE
  64. add sp, sp, a0
  65. /* Start the kernel */
  66. mv a0, s0
  67. mv a1, s1
  68. call parse_dtb
  69. tail start_kernel
  70. relocate:
  71. /* Relocate return address */
  72. li a1, PAGE_OFFSET
  73. la a0, _start
  74. sub a1, a1, a0
  75. add ra, ra, a1
  76. /* Point stvec to virtual address of intruction after satp write */
  77. la a0, 1f
  78. add a0, a0, a1
  79. csrw stvec, a0
  80. /* Compute satp for kernel page tables, but don't load it yet */
  81. la a2, swapper_pg_dir
  82. srl a2, a2, PAGE_SHIFT
  83. li a1, SATP_MODE
  84. or a2, a2, a1
  85. /*
  86. * Load trampoline page directory, which will cause us to trap to
  87. * stvec if VA != PA, or simply fall through if VA == PA
  88. */
  89. la a0, trampoline_pg_dir
  90. srl a0, a0, PAGE_SHIFT
  91. or a0, a0, a1
  92. sfence.vma
  93. csrw sptbr, a0
  94. .align 2
  95. 1:
  96. /* Set trap vector to spin forever to help debug */
  97. la a0, .Lsecondary_park
  98. csrw stvec, a0
  99. /* Reload the global pointer */
  100. .option push
  101. .option norelax
  102. la gp, __global_pointer$
  103. .option pop
  104. /* Switch to kernel page tables */
  105. csrw sptbr, a2
  106. ret
  107. .Lsecondary_start:
  108. #ifdef CONFIG_SMP
  109. li a1, CONFIG_NR_CPUS
  110. bgeu a0, a1, .Lsecondary_park
  111. /* Set trap vector to spin forever to help debug */
  112. la a3, .Lsecondary_park
  113. csrw stvec, a3
  114. slli a3, a0, LGREG
  115. la a1, __cpu_up_stack_pointer
  116. la a2, __cpu_up_task_pointer
  117. add a1, a3, a1
  118. add a2, a3, a2
  119. /*
  120. * This hart didn't win the lottery, so we wait for the winning hart to
  121. * get far enough along the boot process that it should continue.
  122. */
  123. .Lwait_for_cpu_up:
  124. /* FIXME: We should WFI to save some energy here. */
  125. REG_L sp, (a1)
  126. REG_L tp, (a2)
  127. beqz sp, .Lwait_for_cpu_up
  128. beqz tp, .Lwait_for_cpu_up
  129. fence
  130. /* Enable virtual memory and relocate to virtual address */
  131. call relocate
  132. tail smp_callin
  133. #endif
  134. .align 2
  135. .Lsecondary_park:
  136. /* We lack SMP support or have too many harts, so park this hart */
  137. wfi
  138. j .Lsecondary_park
  139. END(_start)
  140. __PAGE_ALIGNED_BSS
  141. /* Empty zero page */
  142. .balign PAGE_SIZE