setup.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/bootmem.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. unsigned long memory_size;
  31. static struct pt_regs fake_regs = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. 0, 0, 0, 0, 0, 0,
  33. 0};
  34. #ifdef CONFIG_VT
  35. struct screen_info screen_info;
  36. #endif
  37. /* Copy a short hook instruction sequence to the exception address */
  38. static inline void copy_exception_handler(unsigned int addr)
  39. {
  40. unsigned int start = (unsigned int) exception_handler_hook;
  41. volatile unsigned int tmp = 0;
  42. if (start == addr) {
  43. /* The CPU exception address already points to the handler. */
  44. return;
  45. }
  46. __asm__ __volatile__ (
  47. "ldw %2,0(%0)\n"
  48. "stw %2,0(%1)\n"
  49. "ldw %2,4(%0)\n"
  50. "stw %2,4(%1)\n"
  51. "ldw %2,8(%0)\n"
  52. "stw %2,8(%1)\n"
  53. "flushd 0(%1)\n"
  54. "flushd 4(%1)\n"
  55. "flushd 8(%1)\n"
  56. "flushi %1\n"
  57. "addi %1,%1,4\n"
  58. "flushi %1\n"
  59. "addi %1,%1,4\n"
  60. "flushi %1\n"
  61. "flushp\n"
  62. : /* no output registers */
  63. : "r" (start), "r" (addr), "r" (tmp)
  64. : "memory"
  65. );
  66. }
  67. /* Copy the fast TLB miss handler */
  68. static inline void copy_fast_tlb_miss_handler(unsigned int addr)
  69. {
  70. unsigned int start = (unsigned int) fast_handler;
  71. unsigned int end = (unsigned int) fast_handler_end;
  72. volatile unsigned int tmp = 0;
  73. __asm__ __volatile__ (
  74. "1:\n"
  75. " ldw %3,0(%0)\n"
  76. " stw %3,0(%1)\n"
  77. " flushd 0(%1)\n"
  78. " flushi %1\n"
  79. " flushp\n"
  80. " addi %0,%0,4\n"
  81. " addi %1,%1,4\n"
  82. " bne %0,%2,1b\n"
  83. : /* no output registers */
  84. : "r" (start), "r" (addr), "r" (end), "r" (tmp)
  85. : "memory"
  86. );
  87. }
  88. /*
  89. * save args passed from u-boot, called from head.S
  90. *
  91. * @r4: NIOS magic
  92. * @r5: initrd start
  93. * @r6: initrd end or fdt
  94. * @r7: kernel command line
  95. */
  96. asmlinkage void __init nios2_boot_init(unsigned r4, unsigned r5, unsigned r6,
  97. unsigned r7)
  98. {
  99. unsigned dtb_passed = 0;
  100. char cmdline_passed[COMMAND_LINE_SIZE] __maybe_unused = { 0, };
  101. #if defined(CONFIG_NIOS2_PASS_CMDLINE)
  102. if (r4 == 0x534f494e) { /* r4 is magic NIOS */
  103. #if defined(CONFIG_BLK_DEV_INITRD)
  104. if (r5) { /* initramfs */
  105. initrd_start = r5;
  106. initrd_end = r6;
  107. }
  108. #endif /* CONFIG_BLK_DEV_INITRD */
  109. dtb_passed = r6;
  110. if (r7)
  111. strncpy(cmdline_passed, (char *)r7, COMMAND_LINE_SIZE);
  112. }
  113. #endif
  114. early_init_devtree((void *)dtb_passed);
  115. #ifndef CONFIG_CMDLINE_FORCE
  116. if (cmdline_passed[0])
  117. strncpy(boot_command_line, cmdline_passed, COMMAND_LINE_SIZE);
  118. #ifdef CONFIG_NIOS2_CMDLINE_IGNORE_DTB
  119. else
  120. strncpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  121. #endif
  122. #endif
  123. parse_early_param();
  124. }
  125. void __init setup_arch(char **cmdline_p)
  126. {
  127. int bootmap_size;
  128. console_verbose();
  129. memory_start = PAGE_ALIGN((unsigned long)__pa(_end));
  130. memory_end = (unsigned long) CONFIG_NIOS2_MEM_BASE + memory_size;
  131. init_mm.start_code = (unsigned long) _stext;
  132. init_mm.end_code = (unsigned long) _etext;
  133. init_mm.end_data = (unsigned long) _edata;
  134. init_mm.brk = (unsigned long) _end;
  135. init_task.thread.kregs = &fake_regs;
  136. /* Keep a copy of command line */
  137. *cmdline_p = boot_command_line;
  138. min_low_pfn = PFN_UP(memory_start);
  139. max_low_pfn = PFN_DOWN(memory_end);
  140. max_mapnr = max_low_pfn;
  141. /*
  142. * give all the memory to the bootmap allocator, tell it to put the
  143. * boot mem_map at the start of memory
  144. */
  145. pr_debug("init_bootmem_node(?,%#lx, %#x, %#lx)\n",
  146. min_low_pfn, PFN_DOWN(PHYS_OFFSET), max_low_pfn);
  147. bootmap_size = init_bootmem_node(NODE_DATA(0),
  148. min_low_pfn, PFN_DOWN(PHYS_OFFSET),
  149. max_low_pfn);
  150. /*
  151. * free the usable memory, we have to make sure we do not free
  152. * the bootmem bitmap so we then reserve it after freeing it :-)
  153. */
  154. pr_debug("free_bootmem(%#lx, %#lx)\n",
  155. memory_start, memory_end - memory_start);
  156. free_bootmem(memory_start, memory_end - memory_start);
  157. /*
  158. * Reserve the bootmem bitmap itself as well. We do this in two
  159. * steps (first step was init_bootmem()) because this catches
  160. * the (very unlikely) case of us accidentally initializing the
  161. * bootmem allocator with an invalid RAM area.
  162. *
  163. * Arguments are start, size
  164. */
  165. pr_debug("reserve_bootmem(%#lx, %#x)\n", memory_start, bootmap_size);
  166. reserve_bootmem(memory_start, bootmap_size, BOOTMEM_DEFAULT);
  167. #ifdef CONFIG_BLK_DEV_INITRD
  168. if (initrd_start) {
  169. reserve_bootmem(virt_to_phys((void *)initrd_start),
  170. initrd_end - initrd_start, BOOTMEM_DEFAULT);
  171. }
  172. #endif /* CONFIG_BLK_DEV_INITRD */
  173. early_init_fdt_reserve_self();
  174. early_init_fdt_scan_reserved_mem();
  175. unflatten_and_copy_device_tree();
  176. setup_cpuinfo();
  177. copy_exception_handler(cpuinfo.exception_addr);
  178. mmu_init();
  179. copy_fast_tlb_miss_handler(cpuinfo.fast_tlb_miss_exc_addr);
  180. /*
  181. * Initialize MMU context handling here because data from cpuinfo is
  182. * needed for this.
  183. */
  184. mmu_context_init();
  185. /*
  186. * get kmalloc into gear
  187. */
  188. paging_init();
  189. #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
  190. conswitchp = &dummy_con;
  191. #endif
  192. }