setup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/bootmem.h>
  22. #include <linux/init.h>
  23. #include <linux/mm.h>
  24. #include <linux/memblock.h>
  25. #include <linux/sched.h>
  26. #include <linux/initrd.h>
  27. #include <linux/console.h>
  28. #include <linux/screen_info.h>
  29. #include <linux/of_fdt.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/sched/task.h>
  32. #include <linux/swiotlb.h>
  33. #include <asm/setup.h>
  34. #include <asm/sections.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/smp.h>
  37. #include <asm/sbi.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/thread_info.h>
  40. #ifdef CONFIG_EARLY_PRINTK
  41. static void sbi_console_write(struct console *co, const char *buf,
  42. unsigned int n)
  43. {
  44. int i;
  45. for (i = 0; i < n; ++i) {
  46. if (buf[i] == '\n')
  47. sbi_console_putchar('\r');
  48. sbi_console_putchar(buf[i]);
  49. }
  50. }
  51. struct console riscv_sbi_early_console_dev __initdata = {
  52. .name = "early",
  53. .write = sbi_console_write,
  54. .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
  55. .index = -1
  56. };
  57. #endif
  58. #ifdef CONFIG_DUMMY_CONSOLE
  59. struct screen_info screen_info = {
  60. .orig_video_lines = 30,
  61. .orig_video_cols = 80,
  62. .orig_video_mode = 0,
  63. .orig_video_ega_bx = 0,
  64. .orig_video_isVGA = 1,
  65. .orig_video_points = 8
  66. };
  67. #endif
  68. unsigned long va_pa_offset;
  69. EXPORT_SYMBOL(va_pa_offset);
  70. unsigned long pfn_base;
  71. EXPORT_SYMBOL(pfn_base);
  72. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
  73. EXPORT_SYMBOL(empty_zero_page);
  74. /* The lucky hart to first increment this variable will boot the other cores */
  75. atomic_t hart_lottery;
  76. #ifdef CONFIG_BLK_DEV_INITRD
  77. static void __init setup_initrd(void)
  78. {
  79. unsigned long size;
  80. if (initrd_start >= initrd_end) {
  81. printk(KERN_INFO "initrd not found or empty");
  82. goto disable;
  83. }
  84. if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
  85. printk(KERN_ERR "initrd extends beyond end of memory");
  86. goto disable;
  87. }
  88. size = initrd_end - initrd_start;
  89. memblock_reserve(__pa(initrd_start), size);
  90. initrd_below_start_ok = 1;
  91. printk(KERN_INFO "Initial ramdisk at: 0x%p (%lu bytes)\n",
  92. (void *)(initrd_start), size);
  93. return;
  94. disable:
  95. pr_cont(" - disabling initrd\n");
  96. initrd_start = 0;
  97. initrd_end = 0;
  98. }
  99. #endif /* CONFIG_BLK_DEV_INITRD */
  100. pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
  101. pgd_t trampoline_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
  102. #ifndef __PAGETABLE_PMD_FOLDED
  103. #define NUM_SWAPPER_PMDS ((uintptr_t)-PAGE_OFFSET >> PGDIR_SHIFT)
  104. pmd_t swapper_pmd[PTRS_PER_PMD*((-PAGE_OFFSET)/PGDIR_SIZE)] __page_aligned_bss;
  105. pmd_t trampoline_pmd[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
  106. #endif
  107. asmlinkage void __init setup_vm(void)
  108. {
  109. extern char _start;
  110. uintptr_t i;
  111. uintptr_t pa = (uintptr_t) &_start;
  112. pgprot_t prot = __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_EXEC);
  113. va_pa_offset = PAGE_OFFSET - pa;
  114. pfn_base = PFN_DOWN(pa);
  115. /* Sanity check alignment and size */
  116. BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
  117. BUG_ON((pa % (PAGE_SIZE * PTRS_PER_PTE)) != 0);
  118. #ifndef __PAGETABLE_PMD_FOLDED
  119. trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
  120. pfn_pgd(PFN_DOWN((uintptr_t)trampoline_pmd),
  121. __pgprot(_PAGE_TABLE));
  122. trampoline_pmd[0] = pfn_pmd(PFN_DOWN(pa), prot);
  123. for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
  124. size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
  125. swapper_pg_dir[o] =
  126. pfn_pgd(PFN_DOWN((uintptr_t)swapper_pmd) + i,
  127. __pgprot(_PAGE_TABLE));
  128. }
  129. for (i = 0; i < ARRAY_SIZE(swapper_pmd); i++)
  130. swapper_pmd[i] = pfn_pmd(PFN_DOWN(pa + i * PMD_SIZE), prot);
  131. #else
  132. trampoline_pg_dir[(PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD] =
  133. pfn_pgd(PFN_DOWN(pa), prot);
  134. for (i = 0; i < (-PAGE_OFFSET)/PGDIR_SIZE; ++i) {
  135. size_t o = (PAGE_OFFSET >> PGDIR_SHIFT) % PTRS_PER_PGD + i;
  136. swapper_pg_dir[o] =
  137. pfn_pgd(PFN_DOWN(pa + i * PGDIR_SIZE), prot);
  138. }
  139. #endif
  140. }
  141. void __init parse_dtb(unsigned int hartid, void *dtb)
  142. {
  143. early_init_dt_scan(__va(dtb));
  144. }
  145. static void __init setup_bootmem(void)
  146. {
  147. struct memblock_region *reg;
  148. phys_addr_t mem_size = 0;
  149. /* Find the memory region containing the kernel */
  150. for_each_memblock(memory, reg) {
  151. phys_addr_t vmlinux_end = __pa(_end);
  152. phys_addr_t end = reg->base + reg->size;
  153. if (reg->base <= vmlinux_end && vmlinux_end <= end) {
  154. /*
  155. * Reserve from the start of the region to the end of
  156. * the kernel
  157. */
  158. memblock_reserve(reg->base, vmlinux_end - reg->base);
  159. mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
  160. }
  161. }
  162. BUG_ON(mem_size == 0);
  163. set_max_mapnr(PFN_DOWN(mem_size));
  164. max_low_pfn = PFN_DOWN(memblock_end_of_DRAM());
  165. max_pfn = max_low_pfn;
  166. #ifdef CONFIG_BLK_DEV_INITRD
  167. setup_initrd();
  168. #endif /* CONFIG_BLK_DEV_INITRD */
  169. early_init_fdt_reserve_self();
  170. early_init_fdt_scan_reserved_mem();
  171. memblock_allow_resize();
  172. memblock_dump_all();
  173. for_each_memblock(memory, reg) {
  174. unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
  175. unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
  176. memblock_set_node(PFN_PHYS(start_pfn),
  177. PFN_PHYS(end_pfn - start_pfn),
  178. &memblock.memory, 0);
  179. }
  180. }
  181. void __init setup_arch(char **cmdline_p)
  182. {
  183. #if defined(CONFIG_EARLY_PRINTK)
  184. if (likely(early_console == NULL)) {
  185. early_console = &riscv_sbi_early_console_dev;
  186. register_console(early_console);
  187. }
  188. #endif
  189. *cmdline_p = boot_command_line;
  190. parse_early_param();
  191. init_mm.start_code = (unsigned long) _stext;
  192. init_mm.end_code = (unsigned long) _etext;
  193. init_mm.end_data = (unsigned long) _edata;
  194. init_mm.brk = (unsigned long) _end;
  195. setup_bootmem();
  196. paging_init();
  197. unflatten_device_tree();
  198. swiotlb_init(1);
  199. #ifdef CONFIG_SMP
  200. setup_smp();
  201. #endif
  202. #ifdef CONFIG_DUMMY_CONSOLE
  203. conswitchp = &dummy_con;
  204. #endif
  205. riscv_fill_hwcap();
  206. }