setup.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  3. * Chen Liqin <liqin.chen@sunplusct.com>
  4. * Lennox Wu <lennox.wu@sunplusct.com>
  5. * Copyright (C) 2012 Regents of the University of California
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see the file COPYING, or write
  19. * to the Free Software Foundation, Inc.,
  20. */
  21. #include <linux/init.h>
  22. #include <linux/mm.h>
  23. #include <linux/memblock.h>
  24. #include <linux/sched.h>
  25. #include <linux/initrd.h>
  26. #include <linux/console.h>
  27. #include <linux/screen_info.h>
  28. #include <linux/of_fdt.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/sched/task.h>
  31. #include <asm/setup.h>
  32. #include <asm/sections.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/smp.h>
  35. #include <asm/sbi.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/thread_info.h>
  38. #ifdef CONFIG_HVC_RISCV_SBI
  39. #include <asm/hvc_riscv_sbi.h>
  40. #endif
  41. #ifdef CONFIG_DUMMY_CONSOLE
  42. struct screen_info screen_info = {
  43. .orig_video_lines = 30,
  44. .orig_video_cols = 80,
  45. .orig_video_mode = 0,
  46. .orig_video_ega_bx = 0,
  47. .orig_video_isVGA = 1,
  48. .orig_video_points = 8
  49. };
  50. #endif
  51. #ifdef CONFIG_CMDLINE_BOOL
  52. static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
  53. #endif /* CONFIG_CMDLINE_BOOL */
  54. unsigned long va_pa_offset;
  55. EXPORT_SYMBOL(va_pa_offset);
  56. unsigned long pfn_base;
  57. EXPORT_SYMBOL(pfn_base);
  58. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
  59. EXPORT_SYMBOL(empty_zero_page);
  60. /* The lucky hart to first increment this variable will boot the other cores */
  61. atomic_t hart_lottery;
  62. #ifdef CONFIG_BLK_DEV_INITRD
  63. static void __init setup_initrd(void)
  64. {
  65. extern char __initramfs_start[];
  66. extern unsigned long __initramfs_size;
  67. unsigned long size;
  68. if (__initramfs_size > 0) {
  69. initrd_start = (unsigned long)(&__initramfs_start);
  70. initrd_end = initrd_start + __initramfs_size;
  71. }
  72. if (initrd_start >= initrd_end) {
  73. printk(KERN_INFO "initrd not found or empty");
  74. goto disable;
  75. }
  76. if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
  77. printk(KERN_ERR "initrd extends beyond end of memory");
  78. goto disable;
  79. }
  80. size = initrd_end - initrd_start;
  81. memblock_reserve(__pa(initrd_start), size);
  82. initrd_below_start_ok = 1;
  83. printk(KERN_INFO "Initial ramdisk at: 0x%p (%lu bytes)\n",
  84. (void *)(initrd_start), size);
  85. return;
  86. disable:
  87. pr_cont(" - disabling initrd\n");
  88. initrd_start = 0;
  89. initrd_end = 0;
  90. }
  91. #endif /* CONFIG_BLK_DEV_INITRD */
  92. pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
  93. pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
  94. #ifndef __PAGETABLE_PMD_FOLDED
  95. #define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT)
  96. pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss;
  97. pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
  98. #endif
  99. asmlinkage void __init setup_vm(void)
  100. {
  101. extern char _start;
  102. uintptr_t i;
  103. uintptr_t pa = (uintptr_t) &_start;
  104. pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
  105. va_pa_offset = PAGE_OFFSET - pa;
  106. pfn_base = PFN_DOWN(pa);
  107. /* Sanity check alignment and size */
  108. BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
  109. BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
  110. #ifndef __PAGETABLE_PMD_FOLDED
  111. trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
  112. pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
  113. __pgprot(_PAGE_TABLE));
  114. trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
  115. for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
  116. size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
  117. swapper_pg_dir[o] =
  118. pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
  119. __pgprot(_PAGE_TABLE));
  120. }
  121. for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
  122. swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
  123. #else
  124. trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
  125. pfn_pgd(PFN_DOWN(pa), prot);
  126. for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
  127. size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
  128. swapper_pg_dir[o] =
  129. pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
  130. }
  131. #endif
  132. }
  133. void __init sbi_save(unsigned int hartid, void *dtb)
  134. {
  135. early_init_dt_scan(__va(dtb));
  136. }
  137. /*
  138. * Allow the user to manually add a memory region (in case DTS is broken);
  139. * "mem_end=nn[KkMmGg]"
  140. */
  141. static int __init mem_end_override(char *p)
  142. {
  143. resource_size_t base, end;
  144. if (!p)
  145. return -EINVAL;
  146. base = (uintptr_t) __pa(PAGE_OFFSET);
  147. end = memparse(p, &p) & PMD_MASK;
  148. if (end == 0)
  149. return -EINVAL;
  150. memblock_add(base, end - base);
  151. return 0;
  152. }
  153. early_param("mem_end", mem_end_override);
  154. static void __init setup_bootmem(void)
  155. {
  156. struct memblock_region *reg;
  157. phys_addr_t mem_size = 0;
  158. /* Find the memory region containing the kernel */
  159. for_each_memblock(memory, reg) {
  160. phys_addr_t vmlinux_end = __pa(_end);
  161. phys_addr_t end = reg->base + reg->size;
  162. if (reg->base <= vmlinux_end && vmlinux_end <= end) {
  163. /*
  164. * Reserve from the start of the region to the end of
  165. * the kernel
  166. */
  167. memblock_reserve(reg->base, vmlinux_end - reg->base);
  168. mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
  169. }
  170. }
  171. BUG_ON(mem_size == 0);
  172. set_max_mapnr(PFN_DOWN(mem_size));
  173. max_low_pfn = pfn_base + PFN_DOWN(mem_size);
  174. #ifdef CONFIG_BLK_DEV_INITRD
  175. setup_initrd();
  176. #endif /* CONFIG_BLK_DEV_INITRD */
  177. early_init_fdt_reserve_self();
  178. early_init_fdt_scan_reserved_mem();
  179. memblock_allow_resize();
  180. memblock_dump_all();
  181. }
  182. void __init setup_arch(char **cmdline_p)
  183. {
  184. #if defined(CONFIG_HVC_RISCV_SBI)
  185. if (likely(early_console == NULL)) {
  186. early_console = &riscv_sbi_early_console_dev;
  187. register_console(early_console);
  188. }
  189. #endif
  190. #ifdef CONFIG_CMDLINE_BOOL
  191. #ifdef CONFIG_CMDLINE_OVERRIDE
  192. strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
  193. #else
  194. if (builtin_cmdline[0] != '\0') {
  195. /* Append bootloader command line to built-in */
  196. strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
  197. strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
  198. strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
  199. }
  200. #endif /* CONFIG_CMDLINE_OVERRIDE */
  201. #endif /* CONFIG_CMDLINE_BOOL */
  202. *cmdline_p = boot_command_line;
  203. parse_early_param();
  204. init_mm.start_code = (unsigned long) _stext;
  205. init_mm.end_code = (unsigned long) _etext;
  206. init_mm.end_data = (unsigned long) _edata;
  207. init_mm.brk = (unsigned long) _end;
  208. setup_bootmem();
  209. paging_init();
  210. unflatten_device_tree();
  211. #ifdef CONFIG_SMP
  212. setup_smp();
  213. #endif
  214. #ifdef CONFIG_DUMMY_CONSOLE
  215. conswitchp = &dummy_con;
  216. #endif
  217. riscv_fill_hwcap();
  218. }
  219. static int __init riscv_device_init(void)
  220. {
  221. return of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
  222. }
  223. subsys_initcall_sync(riscv_device_init);