VPU: return correct decoded length on iommu case
[firefly-linux-kernel-4.4.55.git] / arch / arm / mm / idmap.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/slab.h>
4
5 #include <asm/cputype.h>
6 #include <asm/idmap.h>
7 #include <asm/pgalloc.h>
8 #include <asm/pgtable.h>
9 #include <asm/sections.h>
10 #include <asm/system_info.h>
11
12 pgd_t *idmap_pgd;
13 phys_addr_t (*arch_virt_to_idmap) (unsigned long x);
14
15 #ifdef CONFIG_ARM_LPAE
16 static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
17         unsigned long prot)
18 {
19         pmd_t *pmd;
20         unsigned long next;
21
22         if (pud_none_or_clear_bad(pud) || (pud_val(*pud) & L_PGD_SWAPPER)) {
23                 pmd = pmd_alloc_one(&init_mm, addr);
24                 if (!pmd) {
25                         pr_warning("Failed to allocate identity pmd.\n");
26                         return;
27                 }
28                 /*
29                  * Copy the original PMD to ensure that the PMD entries for
30                  * the kernel image are preserved.
31                  */
32                 if (!pud_none(*pud))
33                         memcpy(pmd, pmd_offset(pud, 0),
34                                PTRS_PER_PMD * sizeof(pmd_t));
35                 pud_populate(&init_mm, pud, pmd);
36                 pmd += pmd_index(addr);
37         } else
38                 pmd = pmd_offset(pud, addr);
39
40         do {
41                 next = pmd_addr_end(addr, end);
42                 *pmd = __pmd((addr & PMD_MASK) | prot);
43                 flush_pmd_entry(pmd);
44         } while (pmd++, addr = next, addr != end);
45 }
46 #else   /* !CONFIG_ARM_LPAE */
47 static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
48         unsigned long prot)
49 {
50         pmd_t *pmd = pmd_offset(pud, addr);
51
52         addr = (addr & PMD_MASK) | prot;
53         pmd[0] = __pmd(addr);
54         addr += SECTION_SIZE;
55         pmd[1] = __pmd(addr);
56         flush_pmd_entry(pmd);
57 }
58 #endif  /* CONFIG_ARM_LPAE */
59
60 static void idmap_add_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
61         unsigned long prot)
62 {
63         pud_t *pud = pud_offset(pgd, addr);
64         unsigned long next;
65
66         do {
67                 next = pud_addr_end(addr, end);
68                 idmap_add_pmd(pud, addr, next, prot);
69         } while (pud++, addr = next, addr != end);
70 }
71
72 static void identity_mapping_add(pgd_t *pgd, const char *text_start,
73                                  const char *text_end, unsigned long prot)
74 {
75         unsigned long addr, end;
76         unsigned long next;
77
78         addr = virt_to_idmap(text_start);
79         end = virt_to_idmap(text_end);
80
81         prot |= PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_AF;
82
83         if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
84                 prot |= PMD_BIT4;
85
86         pgd += pgd_index(addr);
87         do {
88                 next = pgd_addr_end(addr, end);
89                 idmap_add_pud(pgd, addr, next, prot);
90         } while (pgd++, addr = next, addr != end);
91 }
92
93 extern char  __idmap_text_start[], __idmap_text_end[];
94
95 static int __init init_static_idmap(void)
96 {
97         idmap_pgd = pgd_alloc(&init_mm);
98         if (!idmap_pgd)
99                 return -ENOMEM;
100
101         pr_info("Setting up static identity map for 0x%p - 0x%p\n",
102                 __idmap_text_start, __idmap_text_end);
103         identity_mapping_add(idmap_pgd, __idmap_text_start,
104                              __idmap_text_end, 0);
105
106         /* Flush L1 for the hardware to see this page table content */
107         flush_cache_louis();
108
109         return 0;
110 }
111 early_initcall(init_static_idmap);
112
113 /*
114  * In order to soft-boot, we need to switch to a 1:1 mapping for the
115  * cpu_reset functions. This will then ensure that we have predictable
116  * results when turning off the mmu.
117  */
118 void setup_mm_for_reboot(void)
119 {
120         /* Switch to the identity mapping. */
121         cpu_switch_mm(idmap_pgd, &init_mm);
122         local_flush_bp_all();
123
124 #ifdef CONFIG_CPU_HAS_ASID
125         /*
126          * We don't have a clean ASID for the identity mapping, which
127          * may clash with virtual addresses of the previous page tables
128          * and therefore potentially in the TLB.
129          */
130         local_flush_tlb_all();
131 #endif
132 }