init.c 9.9 KB

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