setup.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Nios2-specific parts of system setup
  3. *
  4. * Copyright (C) 2010 Tobias Klauser <[email protected]>
  5. * Copyright (C) 2004 Microtronix Datacom Ltd.
  6. * Copyright (C) 2001 Vic Phillips <[email protected]>
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/export.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/task.h>
  17. #include <linux/console.h>
  18. #include <linux/memblock.h>
  19. #include <linux/initrd.h>
  20. #include <linux/of_fdt.h>
  21. #include <linux/screen_info.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/sections.h>
  24. #include <asm/setup.h>
  25. #include <asm/cpuinfo.h>
  26. unsigned long memory_start;
  27. EXPORT_SYMBOL(memory_start);
  28. unsigned long memory_end;
  29. EXPORT_SYMBOL(memory_end);
  30. static struct pt_regs fake_regs = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31. 0, 0, 0, 0, 0, 0,
  32. 0};
  33. #ifdef CONFIG_VT
  34. struct screen_info screen_info;
  35. #endif
  36. /* Copy a short hook instruction sequence to the exception address */
  37. static inline void copy_exception_handler(unsigned int addr)
  38. {
  39. unsigned int start = (unsigned int) exception_handler_hook;
  40. volatile unsigned int tmp = 0;
  41. if (start == addr) {
  42. /* The CPU exception address already points to the handler. */
  43. return;
  44. }
  45. __asm__ __volatile__ (
  46. "ldw %2,0(%0)\n"
  47. "stw %2,0(%1)\n"
  48. "ldw %2,4(%0)\n"
  49. "stw %2,4(%1)\n"
  50. "ldw %2,8(%0)\n"
  51. "stw %2,8(%1)\n"
  52. "flushd 0(%1)\n"
  53. "flushd 4(%1)\n"
  54. "flushd 8(%1)\n"
  55. "flushi %1\n"
  56. "addi %1,%1,4\n"
  57. "flushi %1\n"
  58. "addi %1,%1,4\n"
  59. "flushi %1\n"
  60. "flushp\n"
  61. : /* no output registers */
  62. : "r" (start), "r" (addr), "r" (tmp)
  63. : "memory"
  64. );
  65. }
  66. /* Copy the fast TLB miss handler */
  67. static inline void copy_fast_tlb_miss_handler(unsigned int addr)
  68. {
  69. unsigned int start = (unsigned int) fast_handler;
  70. unsigned int end = (unsigned int) fast_handler_end;
  71. volatile unsigned int tmp = 0;
  72. __asm__ __volatile__ (
  73. "1:\n"
  74. " ldw %3,0(%0)\n"
  75. " stw %3,0(%1)\n"
  76. " flushd 0(%1)\n"
  77. " flushi %1\n"
  78. " flushp\n"
  79. " addi %0,%0,4\n"
  80. " addi %1,%1,4\n"
  81. " bne %0,%2,1b\n"
  82. : /* no output registers */
  83. : "r" (start), "r" (addr), "r" (end), "r" (tmp)
  84. : "memory"
  85. );
  86. }
  87. /*
  88. * save args passed from u-boot, called from head.S
  89. *
  90. * @r4: NIOS magic
  91. * @r5: initrd start
  92. * @r6: initrd end or fdt
  93. * @r7: kernel command line
  94. */
  95. asmlinkage void __init nios2_boot_init(unsigned r4, unsigned r5, unsigned r6,
  96. unsigned r7)
  97. {
  98. unsigned dtb_passed = 0;
  99. char cmdline_passed[COMMAND_LINE_SIZE] __maybe_unused = { 0, };
  100. #if defined(CONFIG_NIOS2_PASS_CMDLINE)
  101. if (r4 == 0x534f494e) { /* r4 is magic NIOS */
  102. #if defined(CONFIG_BLK_DEV_INITRD)
  103. if (r5) { /* initramfs */
  104. initrd_start = r5;
  105. initrd_end = r6;
  106. }
  107. #endif /* CONFIG_BLK_DEV_INITRD */
  108. dtb_passed = r6;
  109. if (r7)
  110. strlcpy(cmdline_passed, (char *)r7, COMMAND_LINE_SIZE);
  111. }
  112. #endif
  113. early_init_devtree((void *)dtb_passed);
  114. #ifndef CONFIG_CMDLINE_FORCE
  115. if (cmdline_passed[0])
  116. strlcpy(boot_command_line, cmdline_passed, COMMAND_LINE_SIZE);
  117. #ifdef CONFIG_NIOS2_CMDLINE_IGNORE_DTB
  118. else
  119. strlcpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  120. #endif
  121. #endif
  122. parse_early_param();
  123. }
  124. static void __init find_limits(unsigned long *min, unsigned long *max_low,
  125. unsigned long *max_high)
  126. {
  127. *max_low = PFN_DOWN(memblock_get_current_limit());
  128. *min = PFN_UP(memblock_start_of_DRAM());
  129. *max_high = PFN_DOWN(memblock_end_of_DRAM());
  130. }
  131. void __init setup_arch(char **cmdline_p)
  132. {
  133. console_verbose();
  134. memory_start = memblock_start_of_DRAM();
  135. memory_end = memblock_end_of_DRAM();
  136. setup_initial_init_mm(_stext, _etext, _edata, _end);
  137. init_task.thread.kregs = &fake_regs;
  138. /* Keep a copy of command line */
  139. *cmdline_p = boot_command_line;
  140. find_limits(&min_low_pfn, &max_low_pfn, &max_pfn);
  141. max_mapnr = max_low_pfn;
  142. memblock_reserve(__pa_symbol(_stext), _end - _stext);
  143. #ifdef CONFIG_BLK_DEV_INITRD
  144. if (initrd_start) {
  145. memblock_reserve(virt_to_phys((void *)initrd_start),
  146. initrd_end - initrd_start);
  147. }
  148. #endif /* CONFIG_BLK_DEV_INITRD */
  149. early_init_fdt_reserve_self();
  150. early_init_fdt_scan_reserved_mem();
  151. unflatten_and_copy_device_tree();
  152. setup_cpuinfo();
  153. copy_exception_handler(cpuinfo.exception_addr);
  154. mmu_init();
  155. copy_fast_tlb_miss_handler(cpuinfo.fast_tlb_miss_exc_addr);
  156. /*
  157. * Initialize MMU context handling here because data from cpuinfo is
  158. * needed for this.
  159. */
  160. mmu_context_init();
  161. /*
  162. * get kmalloc into gear
  163. */
  164. paging_init();
  165. }