init.c 9.6 KB

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