init.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <phoenix@bsemi.com>
  11. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  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. #include <asm/cacheflush.h>
  37. int mem_init_done;
  38. void __init arch_zone_limits_init(unsigned long *max_zone_pfns)
  39. {
  40. /*
  41. * We use only ZONE_NORMAL
  42. */
  43. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  44. }
  45. extern const char _s_kernel_ro[], _e_kernel_ro[];
  46. /*
  47. * Map all physical memory into kernel's address space.
  48. *
  49. * This is explicitly coded for two-level page tables, so if you need
  50. * something else then this needs to change.
  51. */
  52. static void __init map_ram(void)
  53. {
  54. phys_addr_t start, end;
  55. unsigned long v, p, e;
  56. pgprot_t prot;
  57. pgd_t *pge;
  58. p4d_t *p4e;
  59. pud_t *pue;
  60. pmd_t *pme;
  61. pte_t *pte;
  62. u64 i;
  63. /* These mark extents of read-only kernel pages...
  64. * ...from vmlinux.lds.S
  65. */
  66. v = PAGE_OFFSET;
  67. for_each_mem_range(i, &start, &end) {
  68. p = (u32) start & PAGE_MASK;
  69. e = (u32) end;
  70. v = (u32) __va(p);
  71. pge = pgd_offset_k(v);
  72. while (p < e) {
  73. int j;
  74. p4e = p4d_offset(pge, v);
  75. pue = pud_offset(p4e, v);
  76. pme = pmd_offset(pue, v);
  77. if ((u32) pue != (u32) pge || (u32) pme != (u32) pge) {
  78. panic("%s: OR1K kernel hardcoded for "
  79. "two-level page tables",
  80. __func__);
  81. }
  82. /* Alloc one page for holding PTE's... */
  83. pte = memblock_alloc_raw(PAGE_SIZE, PAGE_SIZE);
  84. if (!pte)
  85. panic("%s: Failed to allocate page for PTEs\n",
  86. __func__);
  87. set_pmd(pme, __pmd(_KERNPG_TABLE + __pa(pte)));
  88. /* Fill the newly allocated page with PTE'S */
  89. for (j = 0; p < e && j < PTRS_PER_PTE;
  90. v += PAGE_SIZE, p += PAGE_SIZE, j++, pte++) {
  91. if (v >= (u32) _e_kernel_ro ||
  92. v < (u32) _s_kernel_ro)
  93. prot = PAGE_KERNEL;
  94. else
  95. prot = PAGE_KERNEL_RO;
  96. set_pte(pte, mk_pte_phys(p, prot));
  97. }
  98. pge++;
  99. }
  100. printk(KERN_INFO "%s: Memory: 0x%x-0x%x\n", __func__,
  101. start, end);
  102. }
  103. }
  104. void __init paging_init(void)
  105. {
  106. int i;
  107. printk(KERN_INFO "Setting up paging and PTEs.\n");
  108. /* clear out the init_mm.pgd that will contain the kernel's mappings */
  109. for (i = 0; i < PTRS_PER_PGD; i++)
  110. swapper_pg_dir[i] = __pgd(0);
  111. /* make sure the current pgd table points to something sane
  112. * (even if it is most probably not used until the next
  113. * switch_mm)
  114. */
  115. current_pgd[smp_processor_id()] = init_mm.pgd;
  116. map_ram();
  117. /* self modifying code ;) */
  118. /* Since the old TLB miss handler has been running up until now,
  119. * the kernel pages are still all RW, so we can still modify the
  120. * text directly... after this change and a TLB flush, the kernel
  121. * pages will become RO.
  122. */
  123. {
  124. extern unsigned long dtlb_miss_handler;
  125. extern unsigned long itlb_miss_handler;
  126. unsigned long *dtlb_vector = __va(0x900);
  127. unsigned long *itlb_vector = __va(0xa00);
  128. printk(KERN_INFO "itlb_miss_handler %p\n", &itlb_miss_handler);
  129. *itlb_vector = ((unsigned long)&itlb_miss_handler -
  130. (unsigned long)itlb_vector) >> 2;
  131. /* Soft ordering constraint to ensure that dtlb_vector is
  132. * the last thing updated
  133. */
  134. barrier();
  135. printk(KERN_INFO "dtlb_miss_handler %p\n", &dtlb_miss_handler);
  136. *dtlb_vector = ((unsigned long)&dtlb_miss_handler -
  137. (unsigned long)dtlb_vector) >> 2;
  138. }
  139. /* Soft ordering constraint to ensure that cache invalidation and
  140. * TLB flush really happen _after_ code has been modified.
  141. */
  142. barrier();
  143. /* Invalidate instruction caches after code modification */
  144. local_icache_block_inv(0x900);
  145. local_icache_block_inv(0xa00);
  146. /* New TLB miss handlers and kernel page tables are in now place.
  147. * Make sure that page flags get updated for all pages in TLB by
  148. * flushing the TLB and forcing all TLB entries to be recreated
  149. * from their page table flags.
  150. */
  151. flush_tlb_all();
  152. }
  153. /* References to section boundaries */
  154. void __init mem_init(void)
  155. {
  156. BUG_ON(!mem_map);
  157. /* clear the zero-page */
  158. memset((void *)empty_zero_page, 0, PAGE_SIZE);
  159. printk("mem_init_done ...........................................\n");
  160. mem_init_done = 1;
  161. return;
  162. }
  163. static int __init map_page(unsigned long va, phys_addr_t pa, pgprot_t prot)
  164. {
  165. p4d_t *p4d;
  166. pud_t *pud;
  167. pmd_t *pmd;
  168. pte_t *pte;
  169. p4d = p4d_offset(pgd_offset_k(va), va);
  170. pud = pud_offset(p4d, va);
  171. pmd = pmd_offset(pud, va);
  172. pte = pte_alloc_kernel(pmd, va);
  173. if (pte == NULL)
  174. return -ENOMEM;
  175. if (pgprot_val(prot))
  176. set_pte_at(&init_mm, va, pte, pfn_pte(pa >> PAGE_SHIFT, prot));
  177. else
  178. pte_clear(&init_mm, va, pte);
  179. local_flush_tlb_page(NULL, va);
  180. return 0;
  181. }
  182. /*
  183. * __set_fix must now support both EARLYCON and TEXT_POKE mappings,
  184. * which are used at different stages of kernel execution.
  185. */
  186. void __set_fixmap(enum fixed_addresses idx,
  187. phys_addr_t phys, pgprot_t prot)
  188. {
  189. unsigned long address = __fix_to_virt(idx);
  190. if (idx >= __end_of_fixed_addresses) {
  191. BUG();
  192. return;
  193. }
  194. map_page(address, phys, prot);
  195. }
  196. static const pgprot_t protection_map[16] = {
  197. [VM_NONE] = PAGE_NONE,
  198. [VM_READ] = PAGE_READONLY_X,
  199. [VM_WRITE] = PAGE_COPY,
  200. [VM_WRITE | VM_READ] = PAGE_COPY_X,
  201. [VM_EXEC] = PAGE_READONLY,
  202. [VM_EXEC | VM_READ] = PAGE_READONLY_X,
  203. [VM_EXEC | VM_WRITE] = PAGE_COPY,
  204. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_X,
  205. [VM_SHARED] = PAGE_NONE,
  206. [VM_SHARED | VM_READ] = PAGE_READONLY_X,
  207. [VM_SHARED | VM_WRITE] = PAGE_SHARED,
  208. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED_X,
  209. [VM_SHARED | VM_EXEC] = PAGE_READONLY,
  210. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READONLY_X,
  211. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED,
  212. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_X
  213. };
  214. DECLARE_VM_GET_PAGE_PROT