setup.c 5.3 KB

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