init.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * linux/arch/arm26/mm/init.c
  3. *
  4. * Copyright (C) 1995-2002 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  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/smp.h>
  21. #include <linux/init.h>
  22. #include <linux/initrd.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/pfn.h>
  26. #include <asm/segment.h>
  27. #include <asm/mach-types.h>
  28. #include <asm/dma.h>
  29. #include <asm/hardware.h>
  30. #include <asm/setup.h>
  31. #include <asm/tlb.h>
  32. #include <asm/map.h>
  33. struct mmu_gather mmu_gathers[NR_CPUS];
  34. extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
  35. extern char _stext, _text, _etext, _end, __init_begin, __init_end;
  36. #ifdef CONFIG_XIP_KERNEL
  37. extern char _endtext, _sdata;
  38. #endif
  39. extern unsigned long phys_initrd_start;
  40. extern unsigned long phys_initrd_size;
  41. /*
  42. * The sole use of this is to pass memory configuration
  43. * data from paging_init to mem_init.
  44. */
  45. static struct meminfo meminfo __initdata = { 0, };
  46. /*
  47. * empty_zero_page is a special page that is used for
  48. * zero-initialized data and COW.
  49. */
  50. struct page *empty_zero_page;
  51. void show_mem(void)
  52. {
  53. int free = 0, total = 0, reserved = 0;
  54. int shared = 0, cached = 0, slab = 0;
  55. struct page *page, *end;
  56. printk("Mem-info:\n");
  57. show_free_areas();
  58. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  59. page = NODE_MEM_MAP(0);
  60. end = page + NODE_DATA(0)->node_spanned_pages;
  61. do {
  62. total++;
  63. if (PageReserved(page))
  64. reserved++;
  65. else if (PageSwapCache(page))
  66. cached++;
  67. else if (PageSlab(page))
  68. slab++;
  69. else if (!page_count(page))
  70. free++;
  71. else
  72. shared += page_count(page) - 1;
  73. page++;
  74. } while (page < end);
  75. printk("%d pages of RAM\n", total);
  76. printk("%d free pages\n", free);
  77. printk("%d reserved pages\n", reserved);
  78. printk("%d slab pages\n", slab);
  79. printk("%d pages shared\n", shared);
  80. printk("%d pages swap cached\n", cached);
  81. }
  82. struct node_info {
  83. unsigned int start;
  84. unsigned int end;
  85. int bootmap_pages;
  86. };
  87. /*
  88. * FIXME: We really want to avoid allocating the bootmap bitmap
  89. * over the top of the initrd. Hopefully, this is located towards
  90. * the start of a bank, so if we allocate the bootmap bitmap at
  91. * the end, we won't clash.
  92. */
  93. static unsigned int __init
  94. find_bootmap_pfn(struct meminfo *mi, unsigned int bootmap_pages)
  95. {
  96. unsigned int start_pfn, bootmap_pfn;
  97. unsigned int start, end;
  98. start_pfn = PFN_UP((unsigned long)&_end);
  99. bootmap_pfn = 0;
  100. /* ARM26 machines only have one node */
  101. if (mi->bank->node != 0)
  102. BUG();
  103. start = PFN_UP(mi->bank->start);
  104. end = PFN_DOWN(mi->bank->size + mi->bank->start);
  105. if (start < start_pfn)
  106. start = start_pfn;
  107. if (end <= start)
  108. BUG();
  109. if (end - start >= bootmap_pages)
  110. bootmap_pfn = start;
  111. else
  112. BUG();
  113. return bootmap_pfn;
  114. }
  115. /*
  116. * Scan the memory info structure and pull out:
  117. * - the end of memory
  118. * - the number of nodes
  119. * - the pfn range of each node
  120. * - the number of bootmem bitmap pages
  121. */
  122. static void __init
  123. find_memend_and_nodes(struct meminfo *mi, struct node_info *np)
  124. {
  125. unsigned int memend_pfn = 0;
  126. nodes_clear(node_online_map);
  127. node_set_online(0);
  128. np->bootmap_pages = 0;
  129. if (mi->bank->size == 0) {
  130. BUG();
  131. }
  132. /*
  133. * Get the start and end pfns for this bank
  134. */
  135. np->start = PFN_UP(mi->bank->start);
  136. np->end = PFN_DOWN(mi->bank->start + mi->bank->size);
  137. if (memend_pfn < np->end)
  138. memend_pfn = np->end;
  139. /*
  140. * Calculate the number of pages we require to
  141. * store the bootmem bitmaps.
  142. */
  143. np->bootmap_pages = bootmem_bootmap_pages(np->end - np->start);
  144. /*
  145. * This doesn't seem to be used by the Linux memory
  146. * manager any more. If we can get rid of it, we
  147. * also get rid of some of the stuff above as well.
  148. */
  149. max_low_pfn = memend_pfn - PFN_DOWN(PHYS_OFFSET);
  150. max_pfn = memend_pfn - PFN_DOWN(PHYS_OFFSET);
  151. mi->end = memend_pfn << PAGE_SHIFT;
  152. }
  153. /*
  154. * Initialise the bootmem allocator for all nodes. This is called
  155. * early during the architecture specific initialisation.
  156. */
  157. void __init bootmem_init(struct meminfo *mi)
  158. {
  159. struct node_info node_info;
  160. unsigned int bootmap_pfn;
  161. pg_data_t *pgdat = NODE_DATA(0);
  162. find_memend_and_nodes(mi, &node_info);
  163. bootmap_pfn = find_bootmap_pfn(mi, node_info.bootmap_pages);
  164. /*
  165. * Note that node 0 must always have some pages.
  166. */
  167. if (node_info.end == 0)
  168. BUG();
  169. /*
  170. * Initialise the bootmem allocator.
  171. */
  172. init_bootmem_node(pgdat, bootmap_pfn, node_info.start, node_info.end);
  173. /*
  174. * Register all available RAM in this node with the bootmem allocator.
  175. */
  176. free_bootmem_node(pgdat, mi->bank->start, mi->bank->size);
  177. /*
  178. * Register the kernel text and data with bootmem.
  179. * Note: with XIP we dont register .text since
  180. * its in ROM.
  181. */
  182. #ifdef CONFIG_XIP_KERNEL
  183. reserve_bootmem_node(pgdat, __pa(&_sdata), &_end - &_sdata);
  184. #else
  185. reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext);
  186. #endif
  187. /*
  188. * And don't forget to reserve the allocator bitmap,
  189. * which will be freed later.
  190. */
  191. reserve_bootmem_node(pgdat, bootmap_pfn << PAGE_SHIFT,
  192. node_info.bootmap_pages << PAGE_SHIFT);
  193. /*
  194. * These should likewise go elsewhere. They pre-reserve
  195. * the screen memory region at the start of main system
  196. * memory. FIXME - screen RAM is not 512K!
  197. */
  198. reserve_bootmem_node(pgdat, 0x02000000, 0x00080000);
  199. #ifdef CONFIG_BLK_DEV_INITRD
  200. initrd_start = phys_initrd_start;
  201. initrd_end = initrd_start + phys_initrd_size;
  202. /* Achimedes machines only have one node, so initrd is in node 0 */
  203. #ifdef CONFIG_XIP_KERNEL
  204. /* Only reserve initrd space if it is in RAM */
  205. if(initrd_start && initrd_start < 0x03000000){
  206. #else
  207. if(initrd_start){
  208. #endif
  209. reserve_bootmem_node(pgdat, __pa(initrd_start),
  210. initrd_end - initrd_start);
  211. }
  212. #endif /* CONFIG_BLK_DEV_INITRD */
  213. }
  214. /*
  215. * paging_init() sets up the page tables, initialises the zone memory
  216. * maps, and sets up the zero page, bad page and bad page tables.
  217. */
  218. void __init paging_init(struct meminfo *mi)
  219. {
  220. void *zero_page;
  221. unsigned long zone_size[MAX_NR_ZONES];
  222. unsigned long zhole_size[MAX_NR_ZONES];
  223. struct bootmem_data *bdata;
  224. pg_data_t *pgdat;
  225. int i;
  226. memcpy(&meminfo, mi, sizeof(meminfo));
  227. /*
  228. * allocate the zero page. Note that we count on this going ok.
  229. */
  230. zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
  231. /*
  232. * initialise the page tables.
  233. */
  234. memtable_init(mi);
  235. flush_tlb_all();
  236. /*
  237. * initialise the zones in node 0 (archimedes have only 1 node)
  238. */
  239. for (i = 0; i < MAX_NR_ZONES; i++) {
  240. zone_size[i] = 0;
  241. zhole_size[i] = 0;
  242. }
  243. pgdat = NODE_DATA(0);
  244. bdata = pgdat->bdata;
  245. zone_size[0] = bdata->node_low_pfn -
  246. (bdata->node_boot_start >> PAGE_SHIFT);
  247. if (!zone_size[0])
  248. BUG();
  249. pgdat->node_mem_map = NULL;
  250. free_area_init_node(0, pgdat, zone_size,
  251. bdata->node_boot_start >> PAGE_SHIFT, zhole_size);
  252. /*
  253. * finish off the bad pages once
  254. * the mem_map is initialised
  255. */
  256. memzero(zero_page, PAGE_SIZE);
  257. empty_zero_page = virt_to_page(zero_page);
  258. }
  259. static inline void free_area(unsigned long addr, unsigned long end, char *s)
  260. {
  261. unsigned int size = (end - addr) >> 10;
  262. for (; addr < end; addr += PAGE_SIZE) {
  263. struct page *page = virt_to_page(addr);
  264. ClearPageReserved(page);
  265. init_page_count(page);
  266. free_page(addr);
  267. totalram_pages++;
  268. }
  269. if (size && s)
  270. printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
  271. }
  272. /*
  273. * mem_init() marks the free areas in the mem_map and tells us how much
  274. * memory is free. This is done after various parts of the system have
  275. * claimed their memory after the kernel image.
  276. */
  277. void __init mem_init(void)
  278. {
  279. unsigned int codepages, datapages, initpages;
  280. pg_data_t *pgdat = NODE_DATA(0);
  281. extern int sysctl_overcommit_memory;
  282. /* Note: data pages includes BSS */
  283. #ifdef CONFIG_XIP_KERNEL
  284. codepages = &_endtext - &_text;
  285. datapages = &_end - &_sdata;
  286. #else
  287. codepages = &_etext - &_text;
  288. datapages = &_end - &_etext;
  289. #endif
  290. initpages = &__init_end - &__init_begin;
  291. high_memory = (void *)__va(meminfo.end);
  292. max_mapnr = virt_to_page(high_memory) - mem_map;
  293. /* this will put all unused low memory onto the freelists */
  294. if (pgdat->node_spanned_pages != 0)
  295. totalram_pages += free_all_bootmem_node(pgdat);
  296. num_physpages = meminfo.bank[0].size >> PAGE_SHIFT;
  297. printk(KERN_INFO "Memory: %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
  298. printk(KERN_NOTICE "Memory: %luKB available (%dK code, "
  299. "%dK data, %dK init)\n",
  300. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  301. codepages >> 10, datapages >> 10, initpages >> 10);
  302. /*
  303. * Turn on overcommit on tiny machines
  304. */
  305. if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
  306. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  307. printk("Turning on overcommit\n");
  308. }
  309. }
  310. void free_initmem(void){
  311. #ifndef CONFIG_XIP_KERNEL
  312. free_area((unsigned long)(&__init_begin),
  313. (unsigned long)(&__init_end),
  314. "init");
  315. #endif
  316. }
  317. #ifdef CONFIG_BLK_DEV_INITRD
  318. static int keep_initrd;
  319. void free_initrd_mem(unsigned long start, unsigned long end)
  320. {
  321. #ifdef CONFIG_XIP_KERNEL
  322. /* Only bin initrd if it is in RAM... */
  323. if(!keep_initrd && start < 0x03000000)
  324. #else
  325. if (!keep_initrd)
  326. #endif
  327. free_area(start, end, "initrd");
  328. }
  329. static int __init keepinitrd_setup(char *__unused)
  330. {
  331. keep_initrd = 1;
  332. return 1;
  333. }
  334. __setup("keepinitrd", keepinitrd_setup);
  335. #endif