init_32.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/sparc/mm/init.c
  4. *
  5. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  6. * Copyright (C) 1995 Eddie C. Dost (ecd@skynet.be)
  7. * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  8. * Copyright (C) 2000 Anton Blanchard (anton@samba.org)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/signal.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/mman.h>
  19. #include <linux/mm.h>
  20. #include <linux/swap.h>
  21. #include <linux/initrd.h>
  22. #include <linux/init.h>
  23. #include <linux/highmem.h>
  24. #include <linux/memblock.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/poison.h>
  27. #include <linux/gfp.h>
  28. #include <asm/sections.h>
  29. #include <asm/page.h>
  30. #include <asm/vaddrs.h>
  31. #include <asm/setup.h>
  32. #include <asm/tlb.h>
  33. #include <asm/prom.h>
  34. #include <asm/leon.h>
  35. #include "mm_32.h"
  36. static unsigned long *sparc_valid_addr_bitmap;
  37. unsigned long phys_base;
  38. EXPORT_SYMBOL(phys_base);
  39. unsigned long pfn_base;
  40. EXPORT_SYMBOL(pfn_base);
  41. struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS+1];
  42. /* Initial ramdisk setup */
  43. extern unsigned int sparc_ramdisk_image;
  44. extern unsigned int sparc_ramdisk_size;
  45. unsigned long highstart_pfn, highend_pfn;
  46. unsigned long last_valid_pfn;
  47. unsigned long calc_highpages(void)
  48. {
  49. int i;
  50. int nr = 0;
  51. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  52. unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
  53. unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
  54. if (end_pfn <= max_low_pfn)
  55. continue;
  56. if (start_pfn < max_low_pfn)
  57. start_pfn = max_low_pfn;
  58. nr += end_pfn - start_pfn;
  59. }
  60. return nr;
  61. }
  62. static unsigned long calc_max_low_pfn(void)
  63. {
  64. int i;
  65. unsigned long tmp = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
  66. unsigned long curr_pfn, last_pfn;
  67. last_pfn = (sp_banks[0].base_addr + sp_banks[0].num_bytes) >> PAGE_SHIFT;
  68. for (i = 1; sp_banks[i].num_bytes != 0; i++) {
  69. curr_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
  70. if (curr_pfn >= tmp) {
  71. if (last_pfn < tmp)
  72. tmp = last_pfn;
  73. break;
  74. }
  75. last_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
  76. }
  77. return tmp;
  78. }
  79. static void __init find_ramdisk(unsigned long end_of_phys_memory)
  80. {
  81. #ifdef CONFIG_BLK_DEV_INITRD
  82. unsigned long size;
  83. /* Now have to check initial ramdisk, so that it won't pass
  84. * the end of memory
  85. */
  86. if (sparc_ramdisk_image) {
  87. if (sparc_ramdisk_image >= (unsigned long)&_end - 2 * PAGE_SIZE)
  88. sparc_ramdisk_image -= KERNBASE;
  89. initrd_start = sparc_ramdisk_image + phys_base;
  90. initrd_end = initrd_start + sparc_ramdisk_size;
  91. if (initrd_end > end_of_phys_memory) {
  92. printk(KERN_CRIT "initrd extends beyond end of memory "
  93. "(0x%016lx > 0x%016lx)\ndisabling initrd\n",
  94. initrd_end, end_of_phys_memory);
  95. initrd_start = 0;
  96. } else {
  97. /* Reserve the initrd image area. */
  98. size = initrd_end - initrd_start;
  99. memblock_reserve(initrd_start, size);
  100. initrd_start = (initrd_start - phys_base) + PAGE_OFFSET;
  101. initrd_end = (initrd_end - phys_base) + PAGE_OFFSET;
  102. }
  103. }
  104. #endif
  105. }
  106. unsigned long __init bootmem_init(unsigned long *pages_avail)
  107. {
  108. unsigned long start_pfn, bytes_avail, size;
  109. unsigned long end_of_phys_memory = 0;
  110. unsigned long high_pages = 0;
  111. int i;
  112. memblock_set_bottom_up(true);
  113. memblock_allow_resize();
  114. bytes_avail = 0UL;
  115. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  116. end_of_phys_memory = sp_banks[i].base_addr +
  117. sp_banks[i].num_bytes;
  118. bytes_avail += sp_banks[i].num_bytes;
  119. if (cmdline_memory_size) {
  120. if (bytes_avail > cmdline_memory_size) {
  121. unsigned long slack = bytes_avail - cmdline_memory_size;
  122. bytes_avail -= slack;
  123. end_of_phys_memory -= slack;
  124. sp_banks[i].num_bytes -= slack;
  125. if (sp_banks[i].num_bytes == 0) {
  126. sp_banks[i].base_addr = 0xdeadbeef;
  127. } else {
  128. memblock_add(sp_banks[i].base_addr,
  129. sp_banks[i].num_bytes);
  130. sp_banks[i+1].num_bytes = 0;
  131. sp_banks[i+1].base_addr = 0xdeadbeef;
  132. }
  133. break;
  134. }
  135. }
  136. memblock_add(sp_banks[i].base_addr, sp_banks[i].num_bytes);
  137. }
  138. /* Start with page aligned address of last symbol in kernel
  139. * image.
  140. */
  141. start_pfn = (unsigned long)__pa(PAGE_ALIGN((unsigned long) &_end));
  142. /* Now shift down to get the real physical page frame number. */
  143. start_pfn >>= PAGE_SHIFT;
  144. max_pfn = end_of_phys_memory >> PAGE_SHIFT;
  145. max_low_pfn = max_pfn;
  146. highstart_pfn = highend_pfn = max_pfn;
  147. if (max_low_pfn > pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT)) {
  148. highstart_pfn = pfn_base + (SRMMU_MAXMEM >> PAGE_SHIFT);
  149. max_low_pfn = calc_max_low_pfn();
  150. high_pages = calc_highpages();
  151. printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
  152. high_pages >> (20 - PAGE_SHIFT));
  153. }
  154. find_ramdisk(end_of_phys_memory);
  155. /* Reserve the kernel text/data/bss. */
  156. size = (start_pfn << PAGE_SHIFT) - phys_base;
  157. memblock_reserve(phys_base, size);
  158. memblock_add(phys_base, size);
  159. size = memblock_phys_mem_size() - memblock_reserved_size();
  160. *pages_avail = (size >> PAGE_SHIFT) - high_pages;
  161. /* Only allow low memory to be allocated via memblock allocation */
  162. memblock_set_current_limit(max_low_pfn << PAGE_SHIFT);
  163. return max_pfn;
  164. }
  165. /*
  166. * paging_init() sets up the page tables: We call the MMU specific
  167. * init routine based upon the Sun model type on the Sparc.
  168. *
  169. */
  170. void __init paging_init(void)
  171. {
  172. srmmu_paging_init();
  173. prom_build_devicetree();
  174. of_fill_in_cpu_data();
  175. device_scan();
  176. }
  177. static void __init taint_real_pages(void)
  178. {
  179. int i;
  180. for (i = 0; sp_banks[i].num_bytes; i++) {
  181. unsigned long start, end;
  182. start = sp_banks[i].base_addr;
  183. end = start + sp_banks[i].num_bytes;
  184. while (start < end) {
  185. set_bit(start >> 20, sparc_valid_addr_bitmap);
  186. start += PAGE_SIZE;
  187. }
  188. }
  189. }
  190. static void map_high_region(unsigned long start_pfn, unsigned long end_pfn)
  191. {
  192. unsigned long tmp;
  193. #ifdef CONFIG_DEBUG_HIGHMEM
  194. printk("mapping high region %08lx - %08lx\n", start_pfn, end_pfn);
  195. #endif
  196. for (tmp = start_pfn; tmp < end_pfn; tmp++)
  197. free_highmem_page(pfn_to_page(tmp));
  198. }
  199. void __init mem_init(void)
  200. {
  201. int i;
  202. if (PKMAP_BASE+LAST_PKMAP*PAGE_SIZE >= FIXADDR_START) {
  203. prom_printf("BUG: fixmap and pkmap areas overlap\n");
  204. prom_printf("pkbase: 0x%lx pkend: 0x%lx fixstart 0x%lx\n",
  205. PKMAP_BASE,
  206. (unsigned long)PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  207. FIXADDR_START);
  208. prom_printf("Please mail sparclinux@vger.kernel.org.\n");
  209. prom_halt();
  210. }
  211. /* Saves us work later. */
  212. memset((void *)empty_zero_page, 0, PAGE_SIZE);
  213. i = last_valid_pfn >> ((20 - PAGE_SHIFT) + 5);
  214. i += 1;
  215. sparc_valid_addr_bitmap = (unsigned long *)
  216. memblock_alloc(i << 2, SMP_CACHE_BYTES);
  217. if (sparc_valid_addr_bitmap == NULL) {
  218. prom_printf("mem_init: Cannot alloc valid_addr_bitmap.\n");
  219. prom_halt();
  220. }
  221. memset(sparc_valid_addr_bitmap, 0, i << 2);
  222. taint_real_pages();
  223. max_mapnr = last_valid_pfn - pfn_base;
  224. high_memory = __va(max_low_pfn << PAGE_SHIFT);
  225. memblock_free_all();
  226. for (i = 0; sp_banks[i].num_bytes != 0; i++) {
  227. unsigned long start_pfn = sp_banks[i].base_addr >> PAGE_SHIFT;
  228. unsigned long end_pfn = (sp_banks[i].base_addr + sp_banks[i].num_bytes) >> PAGE_SHIFT;
  229. if (end_pfn <= highstart_pfn)
  230. continue;
  231. if (start_pfn < highstart_pfn)
  232. start_pfn = highstart_pfn;
  233. map_high_region(start_pfn, end_pfn);
  234. }
  235. }
  236. void sparc_flush_page_to_ram(struct page *page)
  237. {
  238. unsigned long vaddr = (unsigned long)page_address(page);
  239. __flush_page_to_ram(vaddr);
  240. }
  241. EXPORT_SYMBOL(sparc_flush_page_to_ram);
  242. void sparc_flush_folio_to_ram(struct folio *folio)
  243. {
  244. unsigned long vaddr = (unsigned long)folio_address(folio);
  245. unsigned int i, nr = folio_nr_pages(folio);
  246. for (i = 0; i < nr; i++)
  247. __flush_page_to_ram(vaddr + i * PAGE_SIZE);
  248. }
  249. EXPORT_SYMBOL(sparc_flush_folio_to_ram);
  250. static const pgprot_t protection_map[16] = {
  251. [VM_NONE] = PAGE_NONE,
  252. [VM_READ] = PAGE_READONLY,
  253. [VM_WRITE] = PAGE_COPY,
  254. [VM_WRITE | VM_READ] = PAGE_COPY,
  255. [VM_EXEC] = PAGE_READONLY,
  256. [VM_EXEC | VM_READ] = PAGE_READONLY,
  257. [VM_EXEC | VM_WRITE] = PAGE_COPY,
  258. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY,
  259. [VM_SHARED] = PAGE_NONE,
  260. [VM_SHARED | VM_READ] = PAGE_READONLY,
  261. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  262. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
  263. [VM_SHARED | VM_EXEC] = PAGE_READONLY,
  264. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY,
  265. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED,
  266. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED
  267. };
  268. DECLARE_VM_GET_PAGE_PROT