init_32.c 8.9 KB

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