init.c 9.7 KB

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