2 * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 /* Routines for mapping device memory. */
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/devmap.h>
36 #include <vm/vm_extern.h>
38 #include <machine/vmparam.h>
40 static const struct devmap_entry *devmap_table;
41 static boolean_t devmap_bootstrap_done = false;
44 * The allocated-kva (akva) devmap table and metadata. Platforms can call
45 * devmap_add_entry() to add static device mappings to this table using
46 * automatically allocated virtual addresses carved out of the top of kva space.
47 * Allocation begins immediately below the max kernel virtual address.
49 #define AKVA_DEVMAP_MAX_ENTRIES 32
50 static struct devmap_entry akva_devmap_entries[AKVA_DEVMAP_MAX_ENTRIES];
51 static u_int akva_devmap_idx;
52 static vm_offset_t akva_devmap_vaddr = DEVMAP_MAX_VADDR;
54 #if defined(__aarch64__) || defined(__riscv)
55 extern int early_boot;
59 * Print the contents of the static mapping table using the provided printf-like
60 * output function (which will be either printf or db_printf).
63 devmap_dump_table(int (*prfunc)(const char *, ...))
65 const struct devmap_entry *pd;
67 if (devmap_table == NULL || devmap_table[0].pd_size == 0) {
68 prfunc("No static device mappings.\n");
72 prfunc("Static device mappings:\n");
73 for (pd = devmap_table; pd->pd_size != 0; ++pd) {
74 prfunc(" 0x%08jx - 0x%08jx mapped at VA 0x%08jx\n",
76 (uintmax_t)(pd->pd_pa + pd->pd_size - 1),
77 (uintmax_t)pd->pd_va);
82 * Print the contents of the static mapping table. Used for bootverbose.
85 devmap_print_table(void)
87 devmap_dump_table(printf);
91 * Return the "last" kva address used by the registered devmap table. It's
92 * actually the lowest address used by the static mappings, i.e., the address of
93 * the first unusable byte of KVA.
98 const struct devmap_entry *pd;
101 if (akva_devmap_idx > 0)
102 return (akva_devmap_vaddr);
104 lowaddr = DEVMAP_MAX_VADDR;
105 for (pd = devmap_table; pd != NULL && pd->pd_size != 0; ++pd) {
106 if (lowaddr > pd->pd_va)
114 * Add an entry to the internal "akva" static devmap table using the given
115 * physical address and size and a virtual address allocated from the top of
116 * kva. This automatically registers the akva table on the first call, so all a
117 * platform has to do is call this routine to install as many mappings as it
118 * needs and when the platform-specific init function calls devmap_bootstrap()
119 * it will pick up all the entries in the akva table automatically.
122 devmap_add_entry(vm_paddr_t pa, vm_size_t sz)
124 struct devmap_entry *m;
126 if (devmap_bootstrap_done)
127 panic("devmap_add_entry() after devmap_bootstrap()");
129 if (akva_devmap_idx == (AKVA_DEVMAP_MAX_ENTRIES - 1))
130 panic("AKVA_DEVMAP_MAX_ENTRIES is too small");
132 if (akva_devmap_idx == 0)
133 devmap_register_table(akva_devmap_entries);
135 /* Allocate virtual address space from the top of kva downwards. */
138 * If the range being mapped is aligned and sized to 1MB boundaries then
139 * also align the virtual address to the next-lower 1MB boundary so that
140 * we end with a nice efficient section mapping.
142 if ((pa & 0x000fffff) == 0 && (sz & 0x000fffff) == 0) {
143 akva_devmap_vaddr = trunc_1mpage(akva_devmap_vaddr - sz);
147 akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - sz);
149 m = &akva_devmap_entries[akva_devmap_idx++];
150 m->pd_va = akva_devmap_vaddr;
156 * Register the given table as the one to use in devmap_bootstrap().
159 devmap_register_table(const struct devmap_entry *table)
162 devmap_table = table;
166 * Map all of the static regions in the devmap table, and remember the devmap
167 * table so the mapdev, ptov, and vtop functions can do lookups later.
169 * If a non-NULL table pointer is given it is used unconditionally, otherwise
170 * the previously-registered table is used. This smooths transition from legacy
171 * code that fills in a local table then calls this function passing that table,
172 * and newer code that uses devmap_register_table() in platform-specific
173 * code, then lets the common platform-specific init function call this function
174 * with a NULL pointer.
177 devmap_bootstrap(vm_offset_t l1pt, const struct devmap_entry *table)
179 const struct devmap_entry *pd;
181 devmap_bootstrap_done = true;
184 * If given a table pointer, use it. Otherwise, if a table was
185 * previously registered, use it. Otherwise, no work to do.
188 devmap_table = table;
189 else if (devmap_table == NULL)
192 for (pd = devmap_table; pd->pd_size != 0; ++pd) {
194 pmap_preboot_map_attr(pd->pd_pa, pd->pd_va, pd->pd_size,
195 VM_PROT_READ | VM_PROT_WRITE, VM_MEMATTR_DEVICE);
196 #elif defined(__aarch64__) || defined(__riscv)
197 pmap_kenter_device(pd->pd_va, pd->pd_size, pd->pd_pa);
203 * Look up the given physical address in the static mapping data and return the
204 * corresponding virtual address, or NULL if not found.
207 devmap_ptov(vm_paddr_t pa, vm_size_t size)
209 const struct devmap_entry *pd;
211 if (devmap_table == NULL)
214 for (pd = devmap_table; pd->pd_size != 0; ++pd) {
215 if (pa >= pd->pd_pa && pa + size <= pd->pd_pa + pd->pd_size)
216 return ((void *)(pd->pd_va + (pa - pd->pd_pa)));
223 * Look up the given virtual address in the static mapping data and return the
224 * corresponding physical address, or DEVMAP_PADDR_NOTFOUND if not found.
227 devmap_vtop(void * vpva, vm_size_t size)
229 const struct devmap_entry *pd;
232 if (devmap_table == NULL)
233 return (DEVMAP_PADDR_NOTFOUND);
235 va = (vm_offset_t)vpva;
236 for (pd = devmap_table; pd->pd_size != 0; ++pd) {
237 if (va >= pd->pd_va && va + size <= pd->pd_va + pd->pd_size)
238 return ((vm_paddr_t)(pd->pd_pa + (va - pd->pd_va)));
241 return (DEVMAP_PADDR_NOTFOUND);
245 * Map a set of physical memory pages into the kernel virtual address space.
246 * Return a pointer to where it is mapped.
248 * This uses a pre-established static mapping if one exists for the requested
249 * range, otherwise it allocates kva space and maps the physical pages into it.
251 * This routine is intended to be used for mapping device memory, NOT real
252 * memory; the mapping type is inherently VM_MEMATTR_DEVICE in
253 * pmap_kenter_device().
256 pmap_mapdev(vm_paddr_t pa, vm_size_t size)
258 vm_offset_t va, offset;
261 /* First look in the static mapping table. */
262 if ((rva = devmap_ptov(pa, size)) != NULL)
265 offset = pa & PAGE_MASK;
267 size = round_page(size + offset);
269 #if defined(__aarch64__) || defined(__riscv)
271 akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - size);
272 va = akva_devmap_vaddr;
273 KASSERT(va >= VM_MAX_KERNEL_ADDRESS - PMAP_MAPDEV_EARLY_SIZE,
274 ("Too many early devmap mappings"));
277 va = kva_alloc(size);
279 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
281 pmap_kenter_device(va, size, pa);
283 return ((void *)(va + offset));
286 #if defined(__aarch64__) || defined(__riscv)
288 pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
290 vm_offset_t va, offset;
293 /* First look in the static mapping table. */
294 if ((rva = devmap_ptov(pa, size)) != NULL)
297 offset = pa & PAGE_MASK;
299 size = round_page(size + offset);
302 akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - size);
303 va = akva_devmap_vaddr;
304 KASSERT(va >= (VM_MAX_KERNEL_ADDRESS - (PMAP_MAPDEV_EARLY_SIZE)),
305 ("Too many early devmap mappings 2"));
307 va = kva_alloc(size);
309 panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
311 pmap_kenter(va, size, pa, ma);
313 return ((void *)(va + offset));
318 * Unmap device memory and free the kva space.
321 pmap_unmapdev(void *p, vm_size_t size)
323 vm_offset_t offset, va;
325 /* Nothing to do if we find the mapping in the static table. */
326 if (devmap_vtop(p, size) != DEVMAP_PADDR_NOTFOUND)
330 offset = va & PAGE_MASK;
332 size = round_page(size + offset);
334 pmap_kremove_device(va, size);
341 DB_SHOW_COMMAND_FLAGS(devmap, db_show_devmap, DB_CMD_MEMSAFE)
343 devmap_dump_table(db_printf);