1 /*-
2 * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31 * Routines for mapping device memory.
32 *
33 * This is used on both arm and arm64.
34 */
35
36 #include "opt_ddb.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <vm/vm.h>
41 #include <vm/vm_extern.h>
42 #include <vm/pmap.h>
43 #include <machine/armreg.h>
44 #include <machine/devmap.h>
45 #include <machine/vmparam.h>
46
47 static const struct arm_devmap_entry *devmap_table;
48 static boolean_t devmap_bootstrap_done = false;
49
50 #if defined(__aarch64__)
51 #define MAX_VADDR VM_MAX_KERNEL_ADDRESS
52 #define PTE_DEVICE VM_MEMATTR_DEVICE
53 #elif defined(__arm__)
54 #define MAX_VADDR ARM_VECTORS_HIGH
55 #endif
56
57 /*
58 * The allocated-kva (akva) devmap table and metadata. Platforms can call
59 * arm_devmap_add_entry() to add static device mappings to this table using
60 * automatically allocated virtual addresses carved out of the top of kva space.
61 * Allocation begins immediately below the ARM_VECTORS_HIGH address.
62 */
63 #define AKVA_DEVMAP_MAX_ENTRIES 32
64 static struct arm_devmap_entry akva_devmap_entries[AKVA_DEVMAP_MAX_ENTRIES];
65 static u_int akva_devmap_idx;
66 static vm_offset_t akva_devmap_vaddr = MAX_VADDR;
67
68 #ifdef __aarch64__
69 extern int early_boot;
70 #endif
71
72 /*
73 * Print the contents of the static mapping table using the provided printf-like
74 * output function (which will be either printf or db_printf).
75 */
76 static void
devmap_dump_table(int (* prfunc)(const char *,...))77 devmap_dump_table(int (*prfunc)(const char *, ...))
78 {
79 const struct arm_devmap_entry *pd;
80
81 if (devmap_table == NULL || devmap_table[0].pd_size == 0) {
82 prfunc("No static device mappings.\n");
83 return;
84 }
85
86 prfunc("Static device mappings:\n");
87 for (pd = devmap_table; pd->pd_size != 0; ++pd) {
88 prfunc(" 0x%08x - 0x%08x mapped at VA 0x%08x\n",
89 pd->pd_pa, pd->pd_pa + pd->pd_size - 1, pd->pd_va);
90 }
91 }
92
93 /*
94 * Print the contents of the static mapping table. Used for bootverbose.
95 */
96 void
arm_devmap_print_table()97 arm_devmap_print_table()
98 {
99 devmap_dump_table(printf);
100 }
101
102 /*
103 * Return the "last" kva address used by the registered devmap table. It's
104 * actually the lowest address used by the static mappings, i.e., the address of
105 * the first unusable byte of KVA.
106 */
107 vm_offset_t
arm_devmap_lastaddr()108 arm_devmap_lastaddr()
109 {
110 const struct arm_devmap_entry *pd;
111 vm_offset_t lowaddr;
112
113 if (akva_devmap_idx > 0)
114 return (akva_devmap_vaddr);
115
116 lowaddr = MAX_VADDR;
117 for (pd = devmap_table; pd != NULL && pd->pd_size != 0; ++pd) {
118 if (lowaddr > pd->pd_va)
119 lowaddr = pd->pd_va;
120 }
121
122 return (lowaddr);
123 }
124
125 /*
126 * Add an entry to the internal "akva" static devmap table using the given
127 * physical address and size and a virtual address allocated from the top of
128 * kva. This automatically registers the akva table on the first call, so all a
129 * platform has to do is call this routine to install as many mappings as it
130 * needs and when initarm() calls arm_devmap_bootstrap() it will pick up all the
131 * entries in the akva table automatically.
132 */
133 void
arm_devmap_add_entry(vm_paddr_t pa,vm_size_t sz)134 arm_devmap_add_entry(vm_paddr_t pa, vm_size_t sz)
135 {
136 struct arm_devmap_entry *m;
137
138 if (devmap_bootstrap_done)
139 panic("arm_devmap_add_entry() after arm_devmap_bootstrap()");
140
141 if (akva_devmap_idx == (AKVA_DEVMAP_MAX_ENTRIES - 1))
142 panic("AKVA_DEVMAP_MAX_ENTRIES is too small");
143
144 if (akva_devmap_idx == 0)
145 arm_devmap_register_table(akva_devmap_entries);
146
147 /*
148 * Allocate virtual address space from the top of kva downwards. If the
149 * range being mapped is aligned and sized to 1MB boundaries then also
150 * align the virtual address to the next-lower 1MB boundary so that we
151 * end up with a nice efficient section mapping.
152 */
153 #ifdef __arm__
154 if ((pa & 0x000fffff) == 0 && (sz & 0x000fffff) == 0) {
155 akva_devmap_vaddr = trunc_1mpage(akva_devmap_vaddr - sz);
156 } else
157 #endif
158 {
159 akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - sz);
160 }
161 m = &akva_devmap_entries[akva_devmap_idx++];
162 m->pd_va = akva_devmap_vaddr;
163 m->pd_pa = pa;
164 m->pd_size = sz;
165 m->pd_prot = VM_PROT_READ | VM_PROT_WRITE;
166 m->pd_cache = PTE_DEVICE;
167 }
168
169 /*
170 * Register the given table as the one to use in arm_devmap_bootstrap().
171 */
172 void
arm_devmap_register_table(const struct arm_devmap_entry * table)173 arm_devmap_register_table(const struct arm_devmap_entry *table)
174 {
175
176 devmap_table = table;
177 }
178
179 /*
180 * Map all of the static regions in the devmap table, and remember the devmap
181 * table so the mapdev, ptov, and vtop functions can do lookups later.
182 *
183 * If a non-NULL table pointer is given it is used unconditionally, otherwise
184 * the previously-registered table is used. This smooths transition from legacy
185 * code that fills in a local table then calls this function passing that table,
186 * and newer code that uses arm_devmap_register_table() in platform-specific
187 * code, then lets the common initarm() call this function with a NULL pointer.
188 */
189 void
arm_devmap_bootstrap(vm_offset_t l1pt,const struct arm_devmap_entry * table)190 arm_devmap_bootstrap(vm_offset_t l1pt, const struct arm_devmap_entry *table)
191 {
192 const struct arm_devmap_entry *pd;
193
194 devmap_bootstrap_done = true;
195
196 /*
197 * If given a table pointer, use it. Otherwise, if a table was
198 * previously registered, use it. Otherwise, no work to do.
199 */
200 if (table != NULL)
201 devmap_table = table;
202 else if (devmap_table == NULL)
203 return;
204
205 for (pd = devmap_table; pd->pd_size != 0; ++pd) {
206 #if defined(__arm__)
207 pmap_map_chunk(l1pt, pd->pd_va, pd->pd_pa, pd->pd_size,
208 pd->pd_prot,pd->pd_cache);
209 #elif defined(__aarch64__)
210 pmap_kenter_device(pd->pd_va, pd->pd_size, pd->pd_pa);
211 #endif
212 }
213 }
214
215 /*
216 * Look up the given physical address in the static mapping data and return the
217 * corresponding virtual address, or NULL if not found.
218 */
219 void *
arm_devmap_ptov(vm_paddr_t pa,vm_size_t size)220 arm_devmap_ptov(vm_paddr_t pa, vm_size_t size)
221 {
222 const struct arm_devmap_entry *pd;
223
224 if (devmap_table == NULL)
225 return (NULL);
226
227 for (pd = devmap_table; pd->pd_size != 0; ++pd) {
228 if (pa >= pd->pd_pa && pa + size <= pd->pd_pa + pd->pd_size)
229 return ((void *)(pd->pd_va + (pa - pd->pd_pa)));
230 }
231
232 return (NULL);
233 }
234
235 /*
236 * Look up the given virtual address in the static mapping data and return the
237 * corresponding physical address, or DEVMAP_PADDR_NOTFOUND if not found.
238 */
239 vm_paddr_t
arm_devmap_vtop(void * vpva,vm_size_t size)240 arm_devmap_vtop(void * vpva, vm_size_t size)
241 {
242 const struct arm_devmap_entry *pd;
243 vm_offset_t va;
244
245 if (devmap_table == NULL)
246 return (DEVMAP_PADDR_NOTFOUND);
247
248 va = (vm_offset_t)vpva;
249 for (pd = devmap_table; pd->pd_size != 0; ++pd) {
250 if (va >= pd->pd_va && va + size <= pd->pd_va + pd->pd_size)
251 return ((vm_paddr_t)(pd->pd_pa + (va - pd->pd_va)));
252 }
253
254 return (DEVMAP_PADDR_NOTFOUND);
255 }
256
257 /*
258 * Map a set of physical memory pages into the kernel virtual address space.
259 * Return a pointer to where it is mapped.
260 *
261 * This uses a pre-established static mapping if one exists for the requested
262 * range, otherwise it allocates kva space and maps the physical pages into it.
263 *
264 * This routine is intended to be used for mapping device memory, NOT real
265 * memory; the mapping type is inherently PTE_DEVICE in pmap_kenter_device().
266 */
267 void *
pmap_mapdev(vm_offset_t pa,vm_size_t size)268 pmap_mapdev(vm_offset_t pa, vm_size_t size)
269 {
270 vm_offset_t va, offset;
271 void * rva;
272
273 /* First look in the static mapping table. */
274 if ((rva = arm_devmap_ptov(pa, size)) != NULL)
275 return (rva);
276
277 offset = pa & PAGE_MASK;
278 pa = trunc_page(pa);
279 size = round_page(size + offset);
280
281 #ifdef __aarch64__
282 if (early_boot) {
283 akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - size);
284 va = akva_devmap_vaddr;
285 KASSERT(va >= VM_MAX_KERNEL_ADDRESS - L2_SIZE,
286 ("Too many early devmap mappings"));
287 } else
288 #endif
289 va = kva_alloc(size);
290 if (!va)
291 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
292
293 pmap_kenter_device(va, size, pa);
294
295 return ((void *)(va + offset));
296 }
297
298 /*
299 * Unmap device memory and free the kva space.
300 */
301 void
pmap_unmapdev(vm_offset_t va,vm_size_t size)302 pmap_unmapdev(vm_offset_t va, vm_size_t size)
303 {
304 vm_offset_t offset;
305
306 /* Nothing to do if we find the mapping in the static table. */
307 if (arm_devmap_vtop((void*)va, size) != DEVMAP_PADDR_NOTFOUND)
308 return;
309
310 offset = va & PAGE_MASK;
311 va = trunc_page(va);
312 size = round_page(size + offset);
313
314 pmap_kremove_device(va, size);
315 kva_free(va, size);
316 }
317
318 #ifdef DDB
319 #include <ddb/ddb.h>
320
DB_SHOW_COMMAND(devmap,db_show_devmap)321 DB_SHOW_COMMAND(devmap, db_show_devmap)
322 {
323 devmap_dump_table(db_printf);
324 }
325
326 #endif /* DDB */
327
328