init.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * linux/arch/sh/mm/init.c
  3. *
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (C) 2002 - 2007 Paul Mundt
  6. *
  7. * Based on linux/arch/i386/mm/init.c:
  8. * Copyright (C) 1995 Linus Torvalds
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/swap.h>
  12. #include <linux/init.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/percpu.h>
  17. #include <linux/io.h>
  18. #include <linux/dma-mapping.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/tlb.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/sections.h>
  23. #include <asm/cache.h>
  24. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  25. pgd_t swapper_pg_dir[PTRS_PER_PGD];
  26. #ifdef CONFIG_SUPERH32
  27. /*
  28. * Handle trivial transitions between cached and uncached
  29. * segments, making use of the 1:1 mapping relationship in
  30. * 512MB lowmem.
  31. *
  32. * This is the offset of the uncached section from its cached alias.
  33. * Default value only valid in 29 bit mode, in 32bit mode will be
  34. * overridden in pmb_init.
  35. */
  36. unsigned long cached_to_uncached = P2SEG - P1SEG;
  37. #endif
  38. #ifdef CONFIG_MMU
  39. static pte_t *__get_pte_phys(unsigned long addr)
  40. {
  41. pgd_t *pgd;
  42. pud_t *pud;
  43. pmd_t *pmd;
  44. pte_t *pte;
  45. pgd = pgd_offset_k(addr);
  46. if (pgd_none(*pgd)) {
  47. pgd_ERROR(*pgd);
  48. return NULL;
  49. }
  50. pud = pud_alloc(NULL, pgd, addr);
  51. if (unlikely(!pud)) {
  52. pud_ERROR(*pud);
  53. return NULL;
  54. }
  55. pmd = pmd_alloc(NULL, pud, addr);
  56. if (unlikely(!pmd)) {
  57. pmd_ERROR(*pmd);
  58. return NULL;
  59. }
  60. pte = pte_offset_kernel(pmd, addr);
  61. return pte;
  62. }
  63. static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
  64. {
  65. pte_t *pte;
  66. pte = __get_pte_phys(addr);
  67. if (!pte_none(*pte)) {
  68. pte_ERROR(*pte);
  69. return;
  70. }
  71. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
  72. local_flush_tlb_one(get_asid(), addr);
  73. if (pgprot_val(prot) & _PAGE_WIRED)
  74. tlb_wire_entry(NULL, addr, *pte);
  75. }
  76. static void clear_pte_phys(unsigned long addr, pgprot_t prot)
  77. {
  78. pte_t *pte;
  79. pte = __get_pte_phys(addr);
  80. if (pgprot_val(prot) & _PAGE_WIRED)
  81. tlb_unwire_entry();
  82. set_pte(pte, pfn_pte(0, __pgprot(0)));
  83. local_flush_tlb_one(get_asid(), addr);
  84. }
  85. /*
  86. * As a performance optimization, other platforms preserve the fixmap mapping
  87. * across a context switch, we don't presently do this, but this could be done
  88. * in a similar fashion as to the wired TLB interface that sh64 uses (by way
  89. * of the memory mapped UTLB configuration) -- this unfortunately forces us to
  90. * give up a TLB entry for each mapping we want to preserve. While this may be
  91. * viable for a small number of fixmaps, it's not particularly useful for
  92. * everything and needs to be carefully evaluated. (ie, we may want this for
  93. * the vsyscall page).
  94. *
  95. * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
  96. * in at __set_fixmap() time to determine the appropriate behavior to follow.
  97. *
  98. * -- PFM.
  99. */
  100. void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
  101. {
  102. unsigned long address = __fix_to_virt(idx);
  103. if (idx >= __end_of_fixed_addresses) {
  104. BUG();
  105. return;
  106. }
  107. set_pte_phys(address, phys, prot);
  108. }
  109. void __clear_fixmap(enum fixed_addresses idx, pgprot_t prot)
  110. {
  111. unsigned long address = __fix_to_virt(idx);
  112. if (idx >= __end_of_fixed_addresses) {
  113. BUG();
  114. return;
  115. }
  116. clear_pte_phys(address, prot);
  117. }
  118. void __init page_table_range_init(unsigned long start, unsigned long end,
  119. pgd_t *pgd_base)
  120. {
  121. pgd_t *pgd;
  122. pud_t *pud;
  123. pmd_t *pmd;
  124. pte_t *pte;
  125. int i, j, k;
  126. unsigned long vaddr;
  127. vaddr = start;
  128. i = __pgd_offset(vaddr);
  129. j = __pud_offset(vaddr);
  130. k = __pmd_offset(vaddr);
  131. pgd = pgd_base + i;
  132. for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
  133. pud = (pud_t *)pgd;
  134. for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
  135. #ifdef __PAGETABLE_PMD_FOLDED
  136. pmd = (pmd_t *)pud;
  137. #else
  138. pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  139. pud_populate(&init_mm, pud, pmd);
  140. pmd += k;
  141. #endif
  142. for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
  143. if (pmd_none(*pmd)) {
  144. pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
  145. pmd_populate_kernel(&init_mm, pmd, pte);
  146. BUG_ON(pte != pte_offset_kernel(pmd, 0));
  147. }
  148. vaddr += PMD_SIZE;
  149. }
  150. k = 0;
  151. }
  152. j = 0;
  153. }
  154. }
  155. #endif /* CONFIG_MMU */
  156. /*
  157. * paging_init() sets up the page tables
  158. */
  159. void __init paging_init(void)
  160. {
  161. unsigned long max_zone_pfns[MAX_NR_ZONES];
  162. unsigned long vaddr, end;
  163. int nid;
  164. /* We don't need to map the kernel through the TLB, as
  165. * it is permanatly mapped using P1. So clear the
  166. * entire pgd. */
  167. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  168. /* Set an initial value for the MMU.TTB so we don't have to
  169. * check for a null value. */
  170. set_TTB(swapper_pg_dir);
  171. /*
  172. * Populate the relevant portions of swapper_pg_dir so that
  173. * we can use the fixmap entries without calling kmalloc.
  174. * pte's will be filled in by __set_fixmap().
  175. */
  176. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  177. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  178. page_table_range_init(vaddr, end, swapper_pg_dir);
  179. kmap_coherent_init();
  180. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  181. for_each_online_node(nid) {
  182. pg_data_t *pgdat = NODE_DATA(nid);
  183. unsigned long low, start_pfn;
  184. start_pfn = pgdat->bdata->node_min_pfn;
  185. low = pgdat->bdata->node_low_pfn;
  186. if (max_zone_pfns[ZONE_NORMAL] < low)
  187. max_zone_pfns[ZONE_NORMAL] = low;
  188. printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
  189. nid, start_pfn, low);
  190. }
  191. free_area_init_nodes(max_zone_pfns);
  192. /* Set up the uncached fixmap */
  193. set_fixmap_nocache(FIX_UNCACHED, __pa(&__uncached_start));
  194. }
  195. /*
  196. * Early initialization for any I/O MMUs we might have.
  197. */
  198. static void __init iommu_init(void)
  199. {
  200. no_iommu_init();
  201. }
  202. void __init mem_init(void)
  203. {
  204. int codesize, datasize, initsize;
  205. int nid;
  206. iommu_init();
  207. num_physpages = 0;
  208. high_memory = NULL;
  209. for_each_online_node(nid) {
  210. pg_data_t *pgdat = NODE_DATA(nid);
  211. unsigned long node_pages = 0;
  212. void *node_high_memory;
  213. num_physpages += pgdat->node_present_pages;
  214. if (pgdat->node_spanned_pages)
  215. node_pages = free_all_bootmem_node(pgdat);
  216. totalram_pages += node_pages;
  217. node_high_memory = (void *)__va((pgdat->node_start_pfn +
  218. pgdat->node_spanned_pages) <<
  219. PAGE_SHIFT);
  220. if (node_high_memory > high_memory)
  221. high_memory = node_high_memory;
  222. }
  223. /* Set this up early, so we can take care of the zero page */
  224. cpu_cache_init();
  225. /* clear the zero-page */
  226. memset(empty_zero_page, 0, PAGE_SIZE);
  227. __flush_wback_region(empty_zero_page, PAGE_SIZE);
  228. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  229. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  230. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  231. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
  232. "%dk data, %dk init)\n",
  233. nr_free_pages() << (PAGE_SHIFT-10),
  234. num_physpages << (PAGE_SHIFT-10),
  235. codesize >> 10,
  236. datasize >> 10,
  237. initsize >> 10);
  238. /* Initialize the vDSO */
  239. vsyscall_init();
  240. }
  241. void free_initmem(void)
  242. {
  243. unsigned long addr;
  244. addr = (unsigned long)(&__init_begin);
  245. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  246. ClearPageReserved(virt_to_page(addr));
  247. init_page_count(virt_to_page(addr));
  248. free_page(addr);
  249. totalram_pages++;
  250. }
  251. printk("Freeing unused kernel memory: %ldk freed\n",
  252. ((unsigned long)&__init_end -
  253. (unsigned long)&__init_begin) >> 10);
  254. }
  255. #ifdef CONFIG_BLK_DEV_INITRD
  256. void free_initrd_mem(unsigned long start, unsigned long end)
  257. {
  258. unsigned long p;
  259. for (p = start; p < end; p += PAGE_SIZE) {
  260. ClearPageReserved(virt_to_page(p));
  261. init_page_count(virt_to_page(p));
  262. free_page(p);
  263. totalram_pages++;
  264. }
  265. printk("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  266. }
  267. #endif
  268. #ifdef CONFIG_MEMORY_HOTPLUG
  269. int arch_add_memory(int nid, u64 start, u64 size)
  270. {
  271. pg_data_t *pgdat;
  272. unsigned long start_pfn = start >> PAGE_SHIFT;
  273. unsigned long nr_pages = size >> PAGE_SHIFT;
  274. int ret;
  275. pgdat = NODE_DATA(nid);
  276. /* We only have ZONE_NORMAL, so this is easy.. */
  277. ret = __add_pages(nid, pgdat->node_zones + ZONE_NORMAL,
  278. start_pfn, nr_pages);
  279. if (unlikely(ret))
  280. printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
  281. return ret;
  282. }
  283. EXPORT_SYMBOL_GPL(arch_add_memory);
  284. #ifdef CONFIG_NUMA
  285. int memory_add_physaddr_to_nid(u64 addr)
  286. {
  287. /* Node 0 for now.. */
  288. return 0;
  289. }
  290. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  291. #endif
  292. #endif /* CONFIG_MEMORY_HOTPLUG */
  293. #ifdef CONFIG_PMB
  294. int __in_29bit_mode(void)
  295. {
  296. return !(ctrl_inl(PMB_PASCR) & PASCR_SE);
  297. }
  298. #endif /* CONFIG_PMB */