setup.c 6.1 KB

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