1
2
3
4
5
6
7
8#include <linux/sched.h>
9#include <linux/memblock.h>
10#include <asm/pgalloc.h>
11#include <asm/tlb.h>
12#include <asm/dma.h>
13#include <asm/code-patching.h>
14
15#include <mm/mmu_decl.h>
16
17#ifdef CONFIG_SPARSEMEM_VMEMMAP
18
19
20
21
22
23int __meminit vmemmap_create_mapping(unsigned long start,
24 unsigned long page_size,
25 unsigned long phys)
26{
27
28 unsigned long i, flags = _PAGE_PRESENT | _PAGE_ACCESSED |
29 _PAGE_KERNEL_RW;
30
31
32 BUG_ON(mmu_psize_defs[mmu_vmemmap_psize].enc > 0xf);
33
34
35 flags |= mmu_psize_defs[mmu_vmemmap_psize].enc << 8;
36
37
38
39
40
41 for (i = 0; i < page_size; i += PAGE_SIZE)
42 BUG_ON(map_kernel_page(start + i, phys, __pgprot(flags)));
43
44 return 0;
45}
46
47#ifdef CONFIG_MEMORY_HOTPLUG
48void vmemmap_remove_mapping(unsigned long start,
49 unsigned long page_size)
50{
51}
52#endif
53#endif
54
55static void __init *early_alloc_pgtable(unsigned long size)
56{
57 void *ptr;
58
59 ptr = memblock_alloc_try_nid(size, size, MEMBLOCK_LOW_LIMIT,
60 __pa(MAX_DMA_ADDRESS), NUMA_NO_NODE);
61
62 if (!ptr)
63 panic("%s: Failed to allocate %lu bytes align=0x%lx max_addr=%lx\n",
64 __func__, size, size, __pa(MAX_DMA_ADDRESS));
65
66 return ptr;
67}
68
69
70
71
72
73
74int __ref map_kernel_page(unsigned long ea, unsigned long pa, pgprot_t prot)
75{
76 pgd_t *pgdp;
77 p4d_t *p4dp;
78 pud_t *pudp;
79 pmd_t *pmdp;
80 pte_t *ptep;
81
82 BUILD_BUG_ON(TASK_SIZE_USER64 > PGTABLE_RANGE);
83 if (slab_is_available()) {
84 pgdp = pgd_offset_k(ea);
85 p4dp = p4d_offset(pgdp, ea);
86 pudp = pud_alloc(&init_mm, p4dp, ea);
87 if (!pudp)
88 return -ENOMEM;
89 pmdp = pmd_alloc(&init_mm, pudp, ea);
90 if (!pmdp)
91 return -ENOMEM;
92 ptep = pte_alloc_kernel(pmdp, ea);
93 if (!ptep)
94 return -ENOMEM;
95 } else {
96 pgdp = pgd_offset_k(ea);
97 p4dp = p4d_offset(pgdp, ea);
98 if (p4d_none(*p4dp)) {
99 pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
100 p4d_populate(&init_mm, p4dp, pmdp);
101 }
102 pudp = pud_offset(p4dp, ea);
103 if (pud_none(*pudp)) {
104 pmdp = early_alloc_pgtable(PMD_TABLE_SIZE);
105 pud_populate(&init_mm, pudp, pmdp);
106 }
107 pmdp = pmd_offset(pudp, ea);
108 if (!pmd_present(*pmdp)) {
109 ptep = early_alloc_pgtable(PAGE_SIZE);
110 pmd_populate_kernel(&init_mm, pmdp, ptep);
111 }
112 ptep = pte_offset_kernel(pmdp, ea);
113 }
114 set_pte_at(&init_mm, ea, ptep, pfn_pte(pa >> PAGE_SHIFT, prot));
115
116 smp_wmb();
117 return 0;
118}
119
120void __patch_exception(int exc, unsigned long addr)
121{
122 unsigned int *ibase = &interrupt_base_book3e;
123
124
125
126
127
128
129
130
131 patch_branch(ibase + (exc / 4) + 1, addr, 0);
132}
133