init.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/mm.h>
  10. #include <linux/swap.h>
  11. #include <linux/init.h>
  12. #include <linux/mmzone.h>
  13. #include <linux/module.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/nodemask.h>
  17. #include <asm/page.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/tlb.h>
  20. #include <asm/io.h>
  21. #include <asm/dma.h>
  22. #include <asm/setup.h>
  23. #include <asm/sections.h>
  24. #define __page_aligned __attribute__((section(".data.page_aligned")))
  25. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  26. pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned;
  27. struct page *empty_zero_page;
  28. EXPORT_SYMBOL(empty_zero_page);
  29. /*
  30. * Cache of MMU context last used.
  31. */
  32. unsigned long mmu_context_cache = NO_CONTEXT;
  33. /*
  34. * paging_init() sets up the page tables
  35. *
  36. * This routine also unmaps the page at virtual kernel address 0, so
  37. * that we can trap those pesky NULL-reference errors in the kernel.
  38. */
  39. void __init paging_init(void)
  40. {
  41. extern unsigned long _evba;
  42. void *zero_page;
  43. int nid;
  44. /*
  45. * Make sure we can handle exceptions before enabling
  46. * paging. Not that we should ever _get_ any exceptions this
  47. * early, but you never know...
  48. */
  49. printk("Exception vectors start at %p\n", &_evba);
  50. sysreg_write(EVBA, (unsigned long)&_evba);
  51. /*
  52. * Since we are ready to handle exceptions now, we should let
  53. * the CPU generate them...
  54. */
  55. __asm__ __volatile__ ("csrf %0" : : "i"(SR_EM_BIT));
  56. /*
  57. * Allocate the zero page. The allocator will panic if it
  58. * can't satisfy the request, so no need to check.
  59. */
  60. zero_page = alloc_bootmem_low_pages_node(NODE_DATA(0),
  61. PAGE_SIZE);
  62. sysreg_write(PTBR, (unsigned long)swapper_pg_dir);
  63. enable_mmu();
  64. printk ("CPU: Paging enabled\n");
  65. for_each_online_node(nid) {
  66. pg_data_t *pgdat = NODE_DATA(nid);
  67. unsigned long zones_size[MAX_NR_ZONES];
  68. unsigned long low, start_pfn;
  69. start_pfn = pgdat->bdata->node_min_pfn;
  70. low = pgdat->bdata->node_low_pfn;
  71. memset(zones_size, 0, sizeof(zones_size));
  72. zones_size[ZONE_NORMAL] = low - start_pfn;
  73. printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
  74. nid, start_pfn, low);
  75. free_area_init_node(nid, zones_size, start_pfn, NULL);
  76. printk("Node %u: mem_map starts at %p\n",
  77. pgdat->node_id, pgdat->node_mem_map);
  78. }
  79. mem_map = NODE_DATA(0)->node_mem_map;
  80. memset(zero_page, 0, PAGE_SIZE);
  81. empty_zero_page = virt_to_page(zero_page);
  82. flush_dcache_page(empty_zero_page);
  83. }
  84. void __init mem_init(void)
  85. {
  86. int codesize, reservedpages, datasize, initsize;
  87. int nid, i;
  88. reservedpages = 0;
  89. high_memory = NULL;
  90. /* this will put all low memory onto the freelists */
  91. for_each_online_node(nid) {
  92. pg_data_t *pgdat = NODE_DATA(nid);
  93. unsigned long node_pages = 0;
  94. void *node_high_memory;
  95. num_physpages += pgdat->node_present_pages;
  96. if (pgdat->node_spanned_pages != 0)
  97. node_pages = free_all_bootmem_node(pgdat);
  98. totalram_pages += node_pages;
  99. for (i = 0; i < node_pages; i++)
  100. if (PageReserved(pgdat->node_mem_map + i))
  101. reservedpages++;
  102. node_high_memory = (void *)((pgdat->node_start_pfn
  103. + pgdat->node_spanned_pages)
  104. << PAGE_SHIFT);
  105. if (node_high_memory > high_memory)
  106. high_memory = node_high_memory;
  107. }
  108. max_mapnr = MAP_NR(high_memory);
  109. codesize = (unsigned long)_etext - (unsigned long)_text;
  110. datasize = (unsigned long)_edata - (unsigned long)_data;
  111. initsize = (unsigned long)__init_end - (unsigned long)__init_begin;
  112. printk ("Memory: %luk/%luk available (%dk kernel code, "
  113. "%dk reserved, %dk data, %dk init)\n",
  114. (unsigned long)nr_free_pages() << (PAGE_SHIFT - 10),
  115. totalram_pages << (PAGE_SHIFT - 10),
  116. codesize >> 10,
  117. reservedpages << (PAGE_SHIFT - 10),
  118. datasize >> 10,
  119. initsize >> 10);
  120. }
  121. static inline void free_area(unsigned long addr, unsigned long end, char *s)
  122. {
  123. unsigned int size = (end - addr) >> 10;
  124. for (; addr < end; addr += PAGE_SIZE) {
  125. struct page *page = virt_to_page(addr);
  126. ClearPageReserved(page);
  127. init_page_count(page);
  128. free_page(addr);
  129. totalram_pages++;
  130. }
  131. if (size && s)
  132. printk(KERN_INFO "Freeing %s memory: %dK (%lx - %lx)\n",
  133. s, size, end - (size << 10), end);
  134. }
  135. void free_initmem(void)
  136. {
  137. free_area((unsigned long)__init_begin, (unsigned long)__init_end,
  138. "init");
  139. }
  140. #ifdef CONFIG_BLK_DEV_INITRD
  141. void free_initrd_mem(unsigned long start, unsigned long end)
  142. {
  143. free_area(start, end, "initrd");
  144. }
  145. #endif