head.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #include <linux/init.h>
  6. #include <linux/threads.h>
  7. #include <asm/addrspace.h>
  8. #include <asm/asm.h>
  9. #include <asm/asmmacro.h>
  10. #include <asm/bug.h>
  11. #include <asm/regdef.h>
  12. #include <asm/loongarch.h>
  13. #include <asm/stackframe.h>
  14. #ifdef CONFIG_EFI_STUB
  15. #include "efi-header.S"
  16. __HEAD
  17. _head:
  18. .word MZ_MAGIC /* "MZ", MS-DOS header */
  19. .org 0x8
  20. .dword _kernel_entry /* Kernel entry point (physical address) */
  21. .dword _kernel_asize /* Kernel image effective size */
  22. .quad PHYS_LINK_KADDR /* Kernel image load offset from start of RAM */
  23. .org 0x38 /* 0x20 ~ 0x37 reserved */
  24. .long LINUX_PE_MAGIC
  25. .long pe_header - _head /* Offset to the PE header */
  26. pe_header:
  27. __EFI_PE_HEADER
  28. SYM_DATA(kernel_asize, .long _kernel_asize);
  29. SYM_DATA(kernel_fsize, .long _kernel_fsize);
  30. #endif
  31. __REF
  32. .align 12
  33. SYM_CODE_START(kernel_entry) # kernel entry point
  34. /* Config direct window and set PG */
  35. SETUP_DMWINS t0
  36. JUMP_VIRT_ADDR t0, t1
  37. /* Enable PG */
  38. li.w t0, 0xb0 # PLV=0, IE=0, PG=1
  39. csrwr t0, LOONGARCH_CSR_CRMD
  40. li.w t0, 0x04 # PLV=0, PIE=1, PWE=0
  41. csrwr t0, LOONGARCH_CSR_PRMD
  42. li.w t0, 0x00 # FPE=0, SXE=0, ASXE=0, BTE=0
  43. csrwr t0, LOONGARCH_CSR_EUEN
  44. la.pcrel t0, __bss_start # clear .bss
  45. st.d zero, t0, 0
  46. la.pcrel t1, __bss_stop - LONGSIZE
  47. 1:
  48. addi.d t0, t0, LONGSIZE
  49. st.d zero, t0, 0
  50. bne t0, t1, 1b
  51. la.pcrel t0, fw_arg0
  52. st.d a0, t0, 0 # firmware arguments
  53. la.pcrel t0, fw_arg1
  54. st.d a1, t0, 0
  55. la.pcrel t0, fw_arg2
  56. st.d a2, t0, 0
  57. #ifdef CONFIG_PAGE_SIZE_4KB
  58. li.d t0, 0
  59. li.d t1, CSR_STFILL
  60. csrxchg t0, t1, LOONGARCH_CSR_IMPCTL1
  61. #endif
  62. /* KSave3 used for percpu base, initialized as 0 */
  63. csrwr zero, PERCPU_BASE_KS
  64. /* GPR21 used for percpu base (runtime), initialized as 0 */
  65. move u0, zero
  66. la.pcrel tp, init_thread_union
  67. /* Set the SP after an empty pt_regs. */
  68. PTR_LI sp, (_THREAD_SIZE - PT_SIZE)
  69. PTR_ADD sp, sp, tp
  70. set_saved_sp sp, t0, t1
  71. #ifdef CONFIG_RELOCATABLE
  72. bl relocate_kernel
  73. #ifdef CONFIG_RANDOMIZE_BASE
  74. /* Repoint the sp into the new kernel */
  75. PTR_LI sp, (_THREAD_SIZE - PT_SIZE)
  76. PTR_ADD sp, sp, tp
  77. set_saved_sp sp, t0, t1
  78. /* Jump to the new kernel: new_pc = current_pc + random_offset */
  79. pcaddi t0, 0
  80. add.d t0, t0, a0
  81. jirl zero, t0, 0xc
  82. #endif /* CONFIG_RANDOMIZE_BASE */
  83. #endif /* CONFIG_RELOCATABLE */
  84. #ifdef CONFIG_KASAN
  85. bl kasan_early_init
  86. #endif
  87. bl start_kernel
  88. ASM_BUG()
  89. SYM_CODE_END(kernel_entry)
  90. #ifdef CONFIG_SMP
  91. /*
  92. * SMP slave cpus entry point. Board specific code for bootstrap calls this
  93. * function after setting up the stack and tp registers.
  94. */
  95. SYM_CODE_START(smpboot_entry)
  96. SETUP_DMWINS t0
  97. JUMP_VIRT_ADDR t0, t1
  98. #ifdef CONFIG_PAGE_SIZE_4KB
  99. li.d t0, 0
  100. li.d t1, CSR_STFILL
  101. csrxchg t0, t1, LOONGARCH_CSR_IMPCTL1
  102. #endif
  103. /* Enable PG */
  104. li.w t0, 0xb0 # PLV=0, IE=0, PG=1
  105. csrwr t0, LOONGARCH_CSR_CRMD
  106. li.w t0, 0x04 # PLV=0, PIE=1, PWE=0
  107. csrwr t0, LOONGARCH_CSR_PRMD
  108. li.w t0, 0x00 # FPE=0, SXE=0, ASXE=0, BTE=0
  109. csrwr t0, LOONGARCH_CSR_EUEN
  110. la.pcrel t0, cpuboot_data
  111. ld.d sp, t0, CPU_BOOT_STACK
  112. ld.d tp, t0, CPU_BOOT_TINFO
  113. bl start_secondary
  114. ASM_BUG()
  115. SYM_CODE_END(smpboot_entry)
  116. #endif /* CONFIG_SMP */
  117. SYM_ENTRY(kernel_entry_end, SYM_L_GLOBAL, SYM_A_NONE)