Merge tag v3.10.43 into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / mm / ioremap.c
1 /*
2  * Based on arch/arm/mm/ioremap.c
3  *
4  * (C) Copyright 1995 1996 Linus Torvalds
5  * Hacked for ARM by Phil Blundell <philb@gnu.org>
6  * Hacked to allow all architectures to build, and various cleanups
7  * by Russell King
8  * Copyright (C) 2012 ARM Ltd.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include <linux/export.h>
24 #include <linux/mm.h>
25 #include <linux/vmalloc.h>
26 #include <linux/io.h>
27
28 #include <asm/fixmap.h>
29 #include <asm/tlbflush.h>
30 #include <asm/pgalloc.h>
31
32 static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
33                                       pgprot_t prot, void *caller)
34 {
35         unsigned long last_addr;
36         unsigned long offset = phys_addr & ~PAGE_MASK;
37         int err;
38         unsigned long addr;
39         struct vm_struct *area;
40
41         /*
42          * Page align the mapping address and size, taking account of any
43          * offset.
44          */
45         phys_addr &= PAGE_MASK;
46         size = PAGE_ALIGN(size + offset);
47
48         /*
49          * Don't allow wraparound, zero size or outside PHYS_MASK.
50          */
51         last_addr = phys_addr + size - 1;
52         if (!size || last_addr < phys_addr || (last_addr & ~PHYS_MASK))
53                 return NULL;
54
55         /*
56          * Don't allow RAM to be mapped.
57          */
58         if (WARN_ON(pfn_valid(__phys_to_pfn(phys_addr))))
59                 return NULL;
60
61         area = get_vm_area_caller(size, VM_IOREMAP, caller);
62         if (!area)
63                 return NULL;
64         addr = (unsigned long)area->addr;
65
66         err = ioremap_page_range(addr, addr + size, phys_addr, prot);
67         if (err) {
68                 vunmap((void *)addr);
69                 return NULL;
70         }
71
72         return (void __iomem *)(offset + addr);
73 }
74
75 void __iomem *__ioremap(phys_addr_t phys_addr, size_t size, pgprot_t prot)
76 {
77         return __ioremap_caller(phys_addr, size, prot,
78                                 __builtin_return_address(0));
79 }
80 EXPORT_SYMBOL(__ioremap);
81
82 void __iounmap(volatile void __iomem *io_addr)
83 {
84         void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
85
86         vunmap(addr);
87 }
88 EXPORT_SYMBOL(__iounmap);
89
90 void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size)
91 {
92         /* For normal memory we already have a cacheable mapping. */
93         if (pfn_valid(__phys_to_pfn(phys_addr)))
94                 return (void __iomem *)__phys_to_virt(phys_addr);
95
96         return __ioremap_caller(phys_addr, size, __pgprot(PROT_NORMAL),
97                                 __builtin_return_address(0));
98 }
99 EXPORT_SYMBOL(ioremap_cache);
100
101 #ifndef CONFIG_ARM64_64K_PAGES
102 static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
103 #endif
104
105 static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
106 {
107         pgd_t *pgd;
108         pud_t *pud;
109
110         pgd = pgd_offset_k(addr);
111         BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
112
113         pud = pud_offset(pgd, addr);
114         BUG_ON(pud_none(*pud) || pud_bad(*pud));
115
116         return pmd_offset(pud, addr);
117 }
118
119 static inline pte_t * __init early_ioremap_pte(unsigned long addr)
120 {
121         pmd_t *pmd = early_ioremap_pmd(addr);
122
123         BUG_ON(pmd_none(*pmd) || pmd_bad(*pmd));
124
125         return pte_offset_kernel(pmd, addr);
126 }
127
128 void __init early_ioremap_init(void)
129 {
130         pmd_t *pmd;
131
132         pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
133 #ifndef CONFIG_ARM64_64K_PAGES
134         /* need to populate pmd for 4k pagesize only */
135         pmd_populate_kernel(&init_mm, pmd, bm_pte);
136 #endif
137         /*
138          * The boot-ioremap range spans multiple pmds, for which
139          * we are not prepared:
140          */
141         BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
142                      != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
143
144         if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
145                 WARN_ON(1);
146                 pr_warn("pmd %p != %p\n",
147                         pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
148                 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
149                         fix_to_virt(FIX_BTMAP_BEGIN));
150                 pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
151                         fix_to_virt(FIX_BTMAP_END));
152
153                 pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
154                 pr_warn("FIX_BTMAP_BEGIN:     %d\n",
155                         FIX_BTMAP_BEGIN);
156         }
157
158         early_ioremap_setup();
159 }
160
161 void __init __early_set_fixmap(enum fixed_addresses idx,
162                                phys_addr_t phys, pgprot_t flags)
163 {
164         unsigned long addr = __fix_to_virt(idx);
165         pte_t *pte;
166
167         if (idx >= __end_of_fixed_addresses) {
168                 BUG();
169                 return;
170         }
171
172         pte = early_ioremap_pte(addr);
173
174         if (pgprot_val(flags))
175                 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
176         else {
177                 pte_clear(&init_mm, addr, pte);
178                 flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
179         }
180 }