init.c 7.0 KB

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