xref: /freebsd/sys/kern/subr_devmap.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
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 /* Routines for mapping device memory. */
31 
32 #include "opt_ddb.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/devmap.h>
37 #include <vm/vm.h>
38 #include <vm/vm_extern.h>
39 #include <vm/pmap.h>
40 #ifdef __arm__
41 #include <machine/acle-compat.h>
42 #endif
43 #include <machine/vmparam.h>
44 
45 static const struct devmap_entry *devmap_table;
46 static boolean_t devmap_bootstrap_done = false;
47 
48 /*
49  * The allocated-kva (akva) devmap table and metadata.  Platforms can call
50  * devmap_add_entry() to add static device mappings to this table using
51  * automatically allocated virtual addresses carved out of the top of kva space.
52  * Allocation begins immediately below the ARM_VECTORS_HIGH address.
53  */
54 #define	AKVA_DEVMAP_MAX_ENTRIES	32
55 static struct devmap_entry	akva_devmap_entries[AKVA_DEVMAP_MAX_ENTRIES];
56 static u_int			akva_devmap_idx;
57 static vm_offset_t		akva_devmap_vaddr = DEVMAP_MAX_VADDR;
58 
59 #if defined(__aarch64__) || defined(__riscv__)
60 extern int early_boot;
61 #endif
62 
63 /*
64  * Print the contents of the static mapping table using the provided printf-like
65  * output function (which will be either printf or db_printf).
66  */
67 static void
68 devmap_dump_table(int (*prfunc)(const char *, ...))
69 {
70 	const struct devmap_entry *pd;
71 
72 	if (devmap_table == NULL || devmap_table[0].pd_size == 0) {
73 		prfunc("No static device mappings.\n");
74 		return;
75 	}
76 
77 	prfunc("Static device mappings:\n");
78 	for (pd = devmap_table; pd->pd_size != 0; ++pd) {
79 		prfunc("  0x%08x - 0x%08x mapped at VA 0x%08x\n",
80 		    pd->pd_pa, pd->pd_pa + pd->pd_size - 1, pd->pd_va);
81 	}
82 }
83 
84 /*
85  * Print the contents of the static mapping table.  Used for bootverbose.
86  */
87 void
88 devmap_print_table()
89 {
90 	devmap_dump_table(printf);
91 }
92 
93 /*
94  * Return the "last" kva address used by the registered devmap table.  It's
95  * actually the lowest address used by the static mappings, i.e., the address of
96  * the first unusable byte of KVA.
97  */
98 vm_offset_t
99 devmap_lastaddr()
100 {
101 	const struct devmap_entry *pd;
102 	vm_offset_t lowaddr;
103 
104 	if (akva_devmap_idx > 0)
105 		return (akva_devmap_vaddr);
106 
107 	lowaddr = DEVMAP_MAX_VADDR;
108 	for (pd = devmap_table; pd != NULL && pd->pd_size != 0; ++pd) {
109 		if (lowaddr > pd->pd_va)
110 			lowaddr = pd->pd_va;
111 	}
112 
113 	return (lowaddr);
114 }
115 
116 /*
117  * Add an entry to the internal "akva" static devmap table using the given
118  * physical address and size and a virtual address allocated from the top of
119  * kva.  This automatically registers the akva table on the first call, so all a
120  * platform has to do is call this routine to install as many mappings as it
121  * needs and when initarm() calls devmap_bootstrap() it will pick up all the
122  * entries in the akva table automatically.
123  */
124 void
125 devmap_add_entry(vm_paddr_t pa, vm_size_t sz)
126 {
127 	struct devmap_entry *m;
128 
129 	if (devmap_bootstrap_done)
130 		panic("devmap_add_entry() after devmap_bootstrap()");
131 
132 	if (akva_devmap_idx == (AKVA_DEVMAP_MAX_ENTRIES - 1))
133 		panic("AKVA_DEVMAP_MAX_ENTRIES is too small");
134 
135 	if (akva_devmap_idx == 0)
136 		devmap_register_table(akva_devmap_entries);
137 
138 	/*
139 	 * Allocate virtual address space from the top of kva downwards.  If the
140 	 * range being mapped is aligned and sized to 1MB boundaries then also
141 	 * align the virtual address to the next-lower 1MB boundary so that we
142 	 * end up with a nice efficient section mapping.
143 	 */
144 #ifdef __arm__
145 	if ((pa & 0x000fffff) == 0 && (sz & 0x000fffff) == 0) {
146 		akva_devmap_vaddr = trunc_1mpage(akva_devmap_vaddr - sz);
147 	} else
148 #endif
149 	{
150 		akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - sz);
151 	}
152 	m = &akva_devmap_entries[akva_devmap_idx++];
153 	m->pd_va    = akva_devmap_vaddr;
154 	m->pd_pa    = pa;
155 	m->pd_size  = sz;
156 }
157 
158 /*
159  * Register the given table as the one to use in devmap_bootstrap().
160  */
161 void
162 devmap_register_table(const struct devmap_entry *table)
163 {
164 
165 	devmap_table = table;
166 }
167 
168 /*
169  * Map all of the static regions in the devmap table, and remember the devmap
170  * table so the mapdev, ptov, and vtop functions can do lookups later.
171  *
172  * If a non-NULL table pointer is given it is used unconditionally, otherwise
173  * the previously-registered table is used.  This smooths transition from legacy
174  * code that fills in a local table then calls this function passing that table,
175  * and newer code that uses devmap_register_table() in platform-specific
176  * code, then lets the common initarm() call this function with a NULL pointer.
177  */
178 void
179 devmap_bootstrap(vm_offset_t l1pt, const struct devmap_entry *table)
180 {
181 	const struct devmap_entry *pd;
182 
183 	devmap_bootstrap_done = true;
184 
185 	/*
186 	 * If given a table pointer, use it.  Otherwise, if a table was
187 	 * previously registered, use it.  Otherwise, no work to do.
188 	 */
189 	if (table != NULL)
190 		devmap_table = table;
191 	else if (devmap_table == NULL)
192 		return;
193 
194 	for (pd = devmap_table; pd->pd_size != 0; ++pd) {
195 #if defined(__arm__)
196 #if __ARM_ARCH >= 6
197 		pmap_preboot_map_attr(pd->pd_pa, pd->pd_va, pd->pd_size,
198 		    VM_PROT_READ | VM_PROT_WRITE, VM_MEMATTR_DEVICE);
199 #else
200 		pmap_map_chunk(l1pt, pd->pd_va, pd->pd_pa, pd->pd_size,
201 		    VM_PROT_READ | VM_PROT_WRITE, PTE_DEVICE);
202 #endif
203 #elif defined(__aarch64__) || defined(__riscv__)
204 		pmap_kenter_device(pd->pd_va, pd->pd_size, pd->pd_pa);
205 #endif
206 	}
207 }
208 
209 /*
210  * Look up the given physical address in the static mapping data and return the
211  * corresponding virtual address, or NULL if not found.
212  */
213 void *
214 devmap_ptov(vm_paddr_t pa, vm_size_t size)
215 {
216 	const struct devmap_entry *pd;
217 
218 	if (devmap_table == NULL)
219 		return (NULL);
220 
221 	for (pd = devmap_table; pd->pd_size != 0; ++pd) {
222 		if (pa >= pd->pd_pa && pa + size <= pd->pd_pa + pd->pd_size)
223 			return ((void *)(pd->pd_va + (pa - pd->pd_pa)));
224 	}
225 
226 	return (NULL);
227 }
228 
229 /*
230  * Look up the given virtual address in the static mapping data and return the
231  * corresponding physical address, or DEVMAP_PADDR_NOTFOUND if not found.
232  */
233 vm_paddr_t
234 devmap_vtop(void * vpva, vm_size_t size)
235 {
236 	const struct devmap_entry *pd;
237 	vm_offset_t va;
238 
239 	if (devmap_table == NULL)
240 		return (DEVMAP_PADDR_NOTFOUND);
241 
242 	va = (vm_offset_t)vpva;
243 	for (pd = devmap_table; pd->pd_size != 0; ++pd) {
244 		if (va >= pd->pd_va && va + size <= pd->pd_va + pd->pd_size)
245 			return ((vm_paddr_t)(pd->pd_pa + (va - pd->pd_va)));
246 	}
247 
248 	return (DEVMAP_PADDR_NOTFOUND);
249 }
250 
251 /*
252  * Map a set of physical memory pages into the kernel virtual address space.
253  * Return a pointer to where it is mapped.
254  *
255  * This uses a pre-established static mapping if one exists for the requested
256  * range, otherwise it allocates kva space and maps the physical pages into it.
257  *
258  * This routine is intended to be used for mapping device memory, NOT real
259  * memory; the mapping type is inherently VM_MEMATTR_DEVICE in
260  * pmap_kenter_device().
261  */
262 void *
263 pmap_mapdev(vm_offset_t pa, vm_size_t size)
264 {
265 	vm_offset_t va, offset;
266 	void * rva;
267 
268 	/* First look in the static mapping table. */
269 	if ((rva = devmap_ptov(pa, size)) != NULL)
270 		return (rva);
271 
272 	offset = pa & PAGE_MASK;
273 	pa = trunc_page(pa);
274 	size = round_page(size + offset);
275 
276 #if defined(__aarch64__) || defined(__riscv__)
277 	if (early_boot) {
278 		akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - size);
279 		va = akva_devmap_vaddr;
280 		KASSERT(va >= VM_MAX_KERNEL_ADDRESS - L2_SIZE,
281 		    ("Too many early devmap mappings"));
282 	} else
283 #endif
284 		va = kva_alloc(size);
285 	if (!va)
286 		panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
287 
288 	pmap_kenter_device(va, size, pa);
289 
290 	return ((void *)(va + offset));
291 }
292 
293 /*
294  * Unmap device memory and free the kva space.
295  */
296 void
297 pmap_unmapdev(vm_offset_t va, vm_size_t size)
298 {
299 	vm_offset_t offset;
300 
301 	/* Nothing to do if we find the mapping in the static table. */
302 	if (devmap_vtop((void*)va, size) != DEVMAP_PADDR_NOTFOUND)
303 		return;
304 
305 	offset = va & PAGE_MASK;
306 	va = trunc_page(va);
307 	size = round_page(size + offset);
308 
309 	pmap_kremove_device(va, size);
310 	kva_free(va, size);
311 }
312 
313 #ifdef DDB
314 #include <ddb/ddb.h>
315 
316 DB_SHOW_COMMAND(devmap, db_show_devmap)
317 {
318 	devmap_dump_table(db_printf);
319 }
320 
321 #endif /* DDB */
322 
323