init.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OpenRISC idle.c
  4. *
  5. * Linux architectural port borrowing liberally from similar works of
  6. * others. All original copyrights apply as per the original source
  7. * declaration.
  8. *
  9. * Modifications for the OpenRISC architecture:
  10. * Copyright (C) 2003 Matjaz Breskvar <[email protected]>
  11. * Copyright (C) 2010-2011 Jonas Bonn <[email protected]>
  12. */
  13. #include <linux/signal.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/types.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/mman.h>
  21. #include <linux/mm.h>
  22. #include <linux/swap.h>
  23. #include <linux/smp.h>
  24. #include <linux/memblock.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/pagemap.h>
  28. #include <asm/pgalloc.h>
  29. #include <asm/dma.h>
  30. #include <asm/io.h>
  31. #include <asm/tlb.h>
  32. #include <asm/mmu_context.h>
  33. #include <asm/fixmap.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/sections.h>
  36. int mem_init_done;
  37. static void __init zone_sizes_init(void)
  38. {
  39. unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
  40. /*
  41. * We use only ZONE_NORMAL
  42. */
  43. max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
  44. free_area_init(max_zone_pfn);
  45. }
  46. extern const char _s_kernel_ro[], _e_kernel_ro[];
  47. /*
  48. * Map all physical memory into kernel's address space.
  49. *
  50. * This is explicitly coded for two-level page tables, so if you need
  51. * something else then this needs to change.
  52. */
  53. static void __init map_ram(void)
  54. {
  55. phys_addr_t start, end;
  56. unsigned long v, p, e;
  57. pgprot_t prot;
  58. pgd_t *pge;
  59. p4d_t *p4e;
  60. pud_t *pue;
  61. pmd_t *pme;
  62. pte_t *pte;
  63. u64 i;
  64. /* These mark extents of read-only kernel pages...
  65. * ...from vmlinux.lds.S
  66. */
  67. v = PAGE_OFFSET;
  68. for_each_mem_range(i, &start, &end) {
  69. p = (u32) start & PAGE_MASK;
  70. e = (u32) end;
  71. v = (u32) __va(p);
  72. pge = pgd_offset_k(v);
  73. while (p < e) {
  74. int j;
  75. p4e = p4d_offset(pge, v);
  76. pue = pud_offset(p4e, v);
  77. pme = pmd_offset(pue, v);
  78. if ((u32) pue != (u32) pge || (u32) pme != (u32) pge) {
  79. panic("%s: OR1K kernel hardcoded for "
  80. "two-level page tables",
  81. __func__);
  82. }
  83. /* Alloc one page for holding PTE's... */
  84. pte = memblock_alloc_raw(PAGE_SIZE, PAGE_SIZE);
  85. if (!pte)
  86. panic("%s: Failed to allocate page for PTEs\n",
  87. __func__);
  88. set_pmd(pme, __pmd(_KERNPG_TABLE + __pa(pte)));
  89. /* Fill the newly allocated page with PTE'S */
  90. for (j = 0; p < e && j < PTRS_PER_PTE;
  91. v += PAGE_SIZE, p += PAGE_SIZE, j++, pte++) {
  92. if (v >= (u32) _e_kernel_ro ||
  93. v < (u32) _s_kernel_ro)
  94. prot = PAGE_KERNEL;
  95. else
  96. prot = PAGE_KERNEL_RO;
  97. set_pte(pte, mk_pte_phys(p, prot));
  98. }
  99. pge++;
  100. }
  101. printk(KERN_INFO "%s: Memory: 0x%x-0x%x\n", __func__,
  102. start, end);
  103. }
  104. }
  105. void __init paging_init(void)
  106. {
  107. extern void tlb_init(void);
  108. int i;
  109. printk(KERN_INFO "Setting up paging and PTEs.\n");
  110. /* clear out the init_mm.pgd that will contain the kernel's mappings */
  111. for (i = 0; i < PTRS_PER_PGD; i++)
  112. swapper_pg_dir[i] = __pgd(0);
  113. /* make sure the current pgd table points to something sane
  114. * (even if it is most probably not used until the next
  115. * switch_mm)
  116. */
  117. current_pgd[smp_processor_id()] = init_mm.pgd;
  118. map_ram();
  119. zone_sizes_init();
  120. /* self modifying code ;) */
  121. /* Since the old TLB miss handler has been running up until now,
  122. * the kernel pages are still all RW, so we can still modify the
  123. * text directly... after this change and a TLB flush, the kernel
  124. * pages will become RO.
  125. */
  126. {
  127. extern unsigned long dtlb_miss_handler;
  128. extern unsigned long itlb_miss_handler;
  129. unsigned long *dtlb_vector = __va(0x900);
  130. unsigned long *itlb_vector = __va(0xa00);
  131. printk(KERN_INFO "itlb_miss_handler %p\n", &itlb_miss_handler);
  132. *itlb_vector = ((unsigned long)&itlb_miss_handler -
  133. (unsigned long)itlb_vector) >> 2;
  134. /* Soft ordering constraint to ensure that dtlb_vector is
  135. * the last thing updated
  136. */
  137. barrier();
  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. }
  142. /* Soft ordering constraint to ensure that cache invalidation and
  143. * TLB flush really happen _after_ code has been modified.
  144. */
  145. barrier();
  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. void __init mem_init(void)
  158. {
  159. BUG_ON(!mem_map);
  160. max_mapnr = max_low_pfn;
  161. high_memory = (void *)__va(max_low_pfn * PAGE_SIZE);
  162. /* clear the zero-page */
  163. memset((void *)empty_zero_page, 0, PAGE_SIZE);
  164. /* this will put all low memory onto the freelists */
  165. memblock_free_all();
  166. printk("mem_init_done ...........................................\n");
  167. mem_init_done = 1;
  168. return;
  169. }
  170. static const pgprot_t protection_map[16] = {
  171. [VM_NONE] = PAGE_NONE,
  172. [VM_READ] = PAGE_READONLY_X,
  173. [VM_WRITE] = PAGE_COPY,
  174. [VM_WRITE | VM_READ] = PAGE_COPY_X,
  175. [VM_EXEC] = PAGE_READONLY,
  176. [VM_EXEC | VM_READ] = PAGE_READONLY_X,
  177. [VM_EXEC | VM_WRITE] = PAGE_COPY,
  178. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_X,
  179. [VM_SHARED] = PAGE_NONE,
  180. [VM_SHARED | VM_READ] = PAGE_READONLY_X,
  181. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  182. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED_X,
  183. [VM_SHARED | VM_EXEC] = PAGE_READONLY,
  184. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_X,
  185. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED,
  186. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_X
  187. };
  188. DECLARE_VM_GET_PAGE_PROT