init.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * OpenRISC idle.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/signal.h>
  18. #include <linux/sched.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/types.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/mman.h>
  25. #include <linux/mm.h>
  26. #include <linux/swap.h>
  27. #include <linux/smp.h>
  28. #include <linux/bootmem.h>
  29. #include <linux/init.h>
  30. #include <linux/delay.h>
  31. #include <linux/blkdev.h> /* for initrd_* */
  32. #include <linux/pagemap.h>
  33. #include <linux/memblock.h>
  34. #include <asm/segment.h>
  35. #include <asm/pgalloc.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/dma.h>
  38. #include <asm/io.h>
  39. #include <asm/tlb.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/kmap_types.h>
  42. #include <asm/fixmap.h>
  43. #include <asm/tlbflush.h>
  44. int mem_init_done;
  45. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  46. static void __init zone_sizes_init(void)
  47. {
  48. unsigned long zones_size[MAX_NR_ZONES];
  49. /* Clear the zone sizes */
  50. memset(zones_size, 0, sizeof(zones_size));
  51. /*
  52. * We use only ZONE_NORMAL
  53. */
  54. zones_size[ZONE_NORMAL] = max_low_pfn;
  55. free_area_init(zones_size);
  56. }
  57. extern const char _s_kernel_ro[], _e_kernel_ro[];
  58. /*
  59. * Map all physical memory into kernel's address space.
  60. *
  61. * This is explicitly coded for two-level page tables, so if you need
  62. * something else then this needs to change.
  63. */
  64. static void __init map_ram(void)
  65. {
  66. unsigned long v, p, e;
  67. pgprot_t prot;
  68. pgd_t *pge;
  69. pud_t *pue;
  70. pmd_t *pme;
  71. pte_t *pte;
  72. /* These mark extents of read-only kernel pages...
  73. * ...from vmlinux.lds.S
  74. */
  75. struct memblock_region *region;
  76. v = PAGE_OFFSET;
  77. for_each_memblock(memory, region) {
  78. p = (u32) region->base & PAGE_MASK;
  79. e = p + (u32) region->size;
  80. v = (u32) __va(p);
  81. pge = pgd_offset_k(v);
  82. while (p < e) {
  83. int j;
  84. pue = pud_offset(pge, v);
  85. pme = pmd_offset(pue, v);
  86. if ((u32) pue != (u32) pge || (u32) pme != (u32) pge) {
  87. panic("%s: OR1K kernel hardcoded for "
  88. "two-level page tables",
  89. __func__);
  90. }
  91. /* Alloc one page for holding PTE's... */
  92. pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
  93. set_pmd(pme, __pmd(_KERNPG_TABLE + __pa(pte)));
  94. /* Fill the newly allocated page with PTE'S */
  95. for (j = 0; p < e && j < PTRS_PER_PGD;
  96. v += PAGE_SIZE, p += PAGE_SIZE, j++, pte++) {
  97. if (v >= (u32) _e_kernel_ro ||
  98. v < (u32) _s_kernel_ro)
  99. prot = PAGE_KERNEL;
  100. else
  101. prot = PAGE_KERNEL_RO;
  102. set_pte(pte, mk_pte_phys(p, prot));
  103. }
  104. pge++;
  105. }
  106. printk(KERN_INFO "%s: Memory: 0x%x-0x%x\n", __func__,
  107. region->base, region->base + region->size);
  108. }
  109. }
  110. void __init paging_init(void)
  111. {
  112. extern void tlb_init(void);
  113. unsigned long end;
  114. int i;
  115. printk(KERN_INFO "Setting up paging and PTEs.\n");
  116. /* clear out the init_mm.pgd that will contain the kernel's mappings */
  117. for (i = 0; i < PTRS_PER_PGD; i++)
  118. swapper_pg_dir[i] = __pgd(0);
  119. /* make sure the current pgd table points to something sane
  120. * (even if it is most probably not used until the next
  121. * switch_mm)
  122. */
  123. current_pgd = init_mm.pgd;
  124. end = (unsigned long)__va(max_low_pfn * PAGE_SIZE);
  125. map_ram();
  126. zone_sizes_init();
  127. /* self modifying code ;) */
  128. /* Since the old TLB miss handler has been running up until now,
  129. * the kernel pages are still all RW, so we can still modify the
  130. * text directly... after this change and a TLB flush, the kernel
  131. * pages will become RO.
  132. */
  133. {
  134. extern unsigned long dtlb_miss_handler;
  135. extern unsigned long itlb_miss_handler;
  136. unsigned long *dtlb_vector = __va(0x900);
  137. unsigned long *itlb_vector = __va(0xa00);
  138. printk(KERN_INFO "dtlb_miss_handler %p\n", &dtlb_miss_handler);
  139. *dtlb_vector = ((unsigned long)&dtlb_miss_handler -
  140. (unsigned long)dtlb_vector) >> 2;
  141. printk(KERN_INFO "itlb_miss_handler %p\n", &itlb_miss_handler);
  142. *itlb_vector = ((unsigned long)&itlb_miss_handler -
  143. (unsigned long)itlb_vector) >> 2;
  144. }
  145. /* Invalidate instruction caches after code modification */
  146. mtspr(SPR_ICBIR, 0x900);
  147. mtspr(SPR_ICBIR, 0xa00);
  148. /* New TLB miss handlers and kernel page tables are in now place.
  149. * Make sure that page flags get updated for all pages in TLB by
  150. * flushing the TLB and forcing all TLB entries to be recreated
  151. * from their page table flags.
  152. */
  153. flush_tlb_all();
  154. }
  155. /* References to section boundaries */
  156. extern char _stext, _etext, _edata, __bss_start, _end;
  157. extern char __init_begin, __init_end;
  158. static int __init free_pages_init(void)
  159. {
  160. int reservedpages, pfn;
  161. /* this will put all low memory onto the freelists */
  162. totalram_pages = free_all_bootmem();
  163. reservedpages = 0;
  164. for (pfn = 0; pfn < max_low_pfn; pfn++) {
  165. /*
  166. * Only count reserved RAM pages
  167. */
  168. if (PageReserved(mem_map + pfn))
  169. reservedpages++;
  170. }
  171. return reservedpages;
  172. }
  173. static void __init set_max_mapnr_init(void)
  174. {
  175. max_mapnr = num_physpages = max_low_pfn;
  176. }
  177. void __init mem_init(void)
  178. {
  179. int codesize, reservedpages, datasize, initsize;
  180. BUG_ON(!mem_map);
  181. set_max_mapnr_init();
  182. high_memory = (void *)__va(max_low_pfn * PAGE_SIZE);
  183. /* clear the zero-page */
  184. memset((void *)empty_zero_page, 0, PAGE_SIZE);
  185. reservedpages = free_pages_init();
  186. codesize = (unsigned long)&_etext - (unsigned long)&_stext;
  187. datasize = (unsigned long)&_edata - (unsigned long)&_etext;
  188. initsize = (unsigned long)&__init_end - (unsigned long)&__init_begin;
  189. printk(KERN_INFO
  190. "Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data, %dk init, %ldk highmem)\n",
  191. (unsigned long)nr_free_pages() << (PAGE_SHIFT - 10),
  192. max_mapnr << (PAGE_SHIFT - 10), codesize >> 10,
  193. reservedpages << (PAGE_SHIFT - 10), datasize >> 10,
  194. initsize >> 10, (unsigned long)(0 << (PAGE_SHIFT - 10))
  195. );
  196. printk("mem_init_done ...........................................\n");
  197. mem_init_done = 1;
  198. return;
  199. }
  200. #ifdef CONFIG_BLK_DEV_INITRD
  201. void free_initrd_mem(unsigned long start, unsigned long end)
  202. {
  203. printk(KERN_INFO "Freeing initrd memory: %ldk freed\n",
  204. (end - start) >> 10);
  205. for (; start < end; start += PAGE_SIZE) {
  206. ClearPageReserved(virt_to_page(start));
  207. init_page_count(virt_to_page(start));
  208. free_page(start);
  209. totalram_pages++;
  210. }
  211. }
  212. #endif
  213. void free_initmem(void)
  214. {
  215. unsigned long addr;
  216. addr = (unsigned long)(&__init_begin);
  217. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  218. ClearPageReserved(virt_to_page(addr));
  219. init_page_count(virt_to_page(addr));
  220. free_page(addr);
  221. totalram_pages++;
  222. }
  223. printk(KERN_INFO "Freeing unused kernel memory: %luk freed\n",
  224. ((unsigned long)&__init_end -
  225. (unsigned long)&__init_begin) >> 10);
  226. }