setup.c 4.7 KB

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