head.S 3.3 KB

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