head.S 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /* Save hart ID and DTB physical address */
  42. mv s0, a0
  43. mv s1, a1
  44. la a2, boot_cpu_hartid
  45. REG_S a0, (a2)
  46. /* Initialize page tables and relocate to virtual addresses */
  47. la sp, init_thread_union + THREAD_SIZE
  48. call setup_vm
  49. call relocate
  50. /* Restore C environment */
  51. la tp, init_task
  52. sw zero, TASK_TI_CPU(tp)
  53. la sp, init_thread_union
  54. li a0, ASM_THREAD_SIZE
  55. add sp, sp, a0
  56. /* Start the kernel */
  57. mv a0, s0
  58. mv a1, s1
  59. call parse_dtb
  60. tail start_kernel
  61. relocate:
  62. /* Relocate return address */
  63. li a1, PAGE_OFFSET
  64. la a0, _start
  65. sub a1, a1, a0
  66. add ra, ra, a1
  67. /* Point stvec to virtual address of intruction after satp write */
  68. la a0, 1f
  69. add a0, a0, a1
  70. csrw stvec, a0
  71. /* Compute satp for kernel page tables, but don't load it yet */
  72. la a2, swapper_pg_dir
  73. srl a2, a2, PAGE_SHIFT
  74. li a1, SATP_MODE
  75. or a2, a2, a1
  76. /*
  77. * Load trampoline page directory, which will cause us to trap to
  78. * stvec if VA != PA, or simply fall through if VA == PA
  79. */
  80. la a0, trampoline_pg_dir
  81. srl a0, a0, PAGE_SHIFT
  82. or a0, a0, a1
  83. sfence.vma
  84. csrw sptbr, a0
  85. .align 2
  86. 1:
  87. /* Set trap vector to spin forever to help debug */
  88. la a0, .Lsecondary_park
  89. csrw stvec, a0
  90. /* Reload the global pointer */
  91. .option push
  92. .option norelax
  93. la gp, __global_pointer$
  94. .option pop
  95. /* Switch to kernel page tables */
  96. csrw sptbr, a2
  97. ret
  98. .Lsecondary_start:
  99. #ifdef CONFIG_SMP
  100. li a1, CONFIG_NR_CPUS
  101. bgeu a0, a1, .Lsecondary_park
  102. /* Set trap vector to spin forever to help debug */
  103. la a3, .Lsecondary_park
  104. csrw stvec, a3
  105. slli a3, a0, LGREG
  106. la a1, __cpu_up_stack_pointer
  107. la a2, __cpu_up_task_pointer
  108. add a1, a3, a1
  109. add a2, a3, a2
  110. /*
  111. * This hart didn't win the lottery, so we wait for the winning hart to
  112. * get far enough along the boot process that it should continue.
  113. */
  114. .Lwait_for_cpu_up:
  115. /* FIXME: We should WFI to save some energy here. */
  116. REG_L sp, (a1)
  117. REG_L tp, (a2)
  118. beqz sp, .Lwait_for_cpu_up
  119. beqz tp, .Lwait_for_cpu_up
  120. fence
  121. /* Enable virtual memory and relocate to virtual address */
  122. call relocate
  123. tail smp_callin
  124. #endif
  125. .align 2
  126. .Lsecondary_park:
  127. /* We lack SMP support or have too many harts, so park this hart */
  128. wfi
  129. j .Lsecondary_park
  130. END(_start)
  131. __PAGE_ALIGNED_BSS
  132. /* Empty zero page */
  133. .balign PAGE_SIZE