setup.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/console.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/initrd.h>
  19. #include <linux/of_fdt.h>
  20. #include <linux/screen_info.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. unsigned long memory_size;
  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. strncpy(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. strncpy(boot_command_line, cmdline_passed, COMMAND_LINE_SIZE);
  117. #ifdef CONFIG_NIOS2_CMDLINE_IGNORE_DTB
  118. else
  119. strncpy(boot_command_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  120. #endif
  121. #endif
  122. }
  123. void __init setup_arch(char **cmdline_p)
  124. {
  125. int bootmap_size;
  126. console_verbose();
  127. #ifdef CONFIG_EARLY_PRINTK
  128. setup_early_printk();
  129. #endif
  130. memory_start = PAGE_ALIGN((unsigned long)__pa(_end));
  131. memory_end = (unsigned long) CONFIG_NIOS2_MEM_BASE + memory_size;
  132. init_mm.start_code = (unsigned long) _stext;
  133. init_mm.end_code = (unsigned long) _etext;
  134. init_mm.end_data = (unsigned long) _edata;
  135. init_mm.brk = (unsigned long) _end;
  136. init_task.thread.kregs = &fake_regs;
  137. /* Keep a copy of command line */
  138. *cmdline_p = boot_command_line;
  139. min_low_pfn = PFN_UP(memory_start);
  140. max_low_pfn = PFN_DOWN(memory_end);
  141. max_mapnr = max_low_pfn;
  142. /*
  143. * give all the memory to the bootmap allocator, tell it to put the
  144. * boot mem_map at the start of memory
  145. */
  146. pr_debug("init_bootmem_node(?,%#lx, %#x, %#lx)\n",
  147. min_low_pfn, PFN_DOWN(PHYS_OFFSET), max_low_pfn);
  148. bootmap_size = init_bootmem_node(NODE_DATA(0),
  149. min_low_pfn, PFN_DOWN(PHYS_OFFSET),
  150. max_low_pfn);
  151. /*
  152. * free the usable memory, we have to make sure we do not free
  153. * the bootmem bitmap so we then reserve it after freeing it :-)
  154. */
  155. pr_debug("free_bootmem(%#lx, %#lx)\n",
  156. memory_start, memory_end - memory_start);
  157. free_bootmem(memory_start, memory_end - memory_start);
  158. /*
  159. * Reserve the bootmem bitmap itself as well. We do this in two
  160. * steps (first step was init_bootmem()) because this catches
  161. * the (very unlikely) case of us accidentally initializing the
  162. * bootmem allocator with an invalid RAM area.
  163. *
  164. * Arguments are start, size
  165. */
  166. pr_debug("reserve_bootmem(%#lx, %#x)\n", memory_start, bootmap_size);
  167. reserve_bootmem(memory_start, bootmap_size, BOOTMEM_DEFAULT);
  168. #ifdef CONFIG_BLK_DEV_INITRD
  169. if (initrd_start) {
  170. reserve_bootmem(virt_to_phys((void *)initrd_start),
  171. initrd_end - initrd_start, BOOTMEM_DEFAULT);
  172. }
  173. #endif /* CONFIG_BLK_DEV_INITRD */
  174. unflatten_and_copy_device_tree();
  175. setup_cpuinfo();
  176. copy_exception_handler(cpuinfo.exception_addr);
  177. mmu_init();
  178. copy_fast_tlb_miss_handler(cpuinfo.fast_tlb_miss_exc_addr);
  179. /*
  180. * Initialize MMU context handling here because data from cpuinfo is
  181. * needed for this.
  182. */
  183. mmu_context_init();
  184. /*
  185. * get kmalloc into gear
  186. */
  187. paging_init();
  188. #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
  189. conswitchp = &dummy_con;
  190. #endif
  191. }