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 sbi_save
  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 sptbr write */
  66. la a0, 1f
  67. add a0, a0, a1
  68. csrw stvec, a0
  69. /* Compute sptbr 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, SPTBR_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. 1:
  84. /* Set trap vector to spin forever to help debug */
  85. la a0, .Lsecondary_park
  86. csrw stvec, a0
  87. /* Reload the global pointer */
  88. .option push
  89. .option norelax
  90. la gp, __global_pointer$
  91. .option pop
  92. /* Switch to kernel page tables */
  93. csrw sptbr, a2
  94. ret
  95. .Lsecondary_start:
  96. #ifdef CONFIG_SMP
  97. li a1, CONFIG_NR_CPUS
  98. bgeu a0, a1, .Lsecondary_park
  99. /* Set trap vector to spin forever to help debug */
  100. la a3, .Lsecondary_park
  101. csrw stvec, a3
  102. slli a3, a0, LGREG
  103. la a1, __cpu_up_stack_pointer
  104. la a2, __cpu_up_task_pointer
  105. add a1, a3, a1
  106. add a2, a3, a2
  107. /*
  108. * This hart didn't win the lottery, so we wait for the winning hart to
  109. * get far enough along the boot process that it should continue.
  110. */
  111. .Lwait_for_cpu_up:
  112. /* FIXME: We should WFI to save some energy here. */
  113. REG_L sp, (a1)
  114. REG_L tp, (a2)
  115. beqz sp, .Lwait_for_cpu_up
  116. beqz tp, .Lwait_for_cpu_up
  117. fence
  118. /* Enable virtual memory and relocate to virtual address */
  119. call relocate
  120. tail smp_callin
  121. #endif
  122. .Lsecondary_park:
  123. /* We lack SMP support or have too many harts, so park this hart */
  124. wfi
  125. j .Lsecondary_park
  126. END(_start)
  127. __PAGE_ALIGNED_BSS
  128. /* Empty zero page */
  129. .balign PAGE_SIZE
  130. ENTRY(empty_zero_page)
  131. .fill (empty_zero_page + PAGE_SIZE) - ., 1, 0x00
  132. END(empty_zero_page)