113a98c85SIan Lepore /*-
213a98c85SIan Lepore * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
313a98c85SIan Lepore * All rights reserved.
413a98c85SIan Lepore *
513a98c85SIan Lepore * Redistribution and use in source and binary forms, with or without
613a98c85SIan Lepore * modification, are permitted provided that the following conditions
713a98c85SIan Lepore * are met:
813a98c85SIan Lepore * 1. Redistributions of source code must retain the above copyright
913a98c85SIan Lepore * notice, this list of conditions and the following disclaimer.
1013a98c85SIan Lepore * 2. Redistributions in binary form must reproduce the above copyright
1113a98c85SIan Lepore * notice, this list of conditions and the following disclaimer in the
1213a98c85SIan Lepore * documentation and/or other materials provided with the distribution.
1313a98c85SIan Lepore *
1413a98c85SIan Lepore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1513a98c85SIan Lepore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1613a98c85SIan Lepore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1713a98c85SIan Lepore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1813a98c85SIan Lepore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1913a98c85SIan Lepore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2013a98c85SIan Lepore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2113a98c85SIan Lepore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2213a98c85SIan Lepore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2313a98c85SIan Lepore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2413a98c85SIan Lepore * SUCH DAMAGE.
2513a98c85SIan Lepore */
2613a98c85SIan Lepore
2713a98c85SIan Lepore #include <sys/cdefs.h>
2830b72b68SRuslan Bukin /* Routines for mapping device memory. */
2913a98c85SIan Lepore
30a0e04ab3SIan Lepore #include "opt_ddb.h"
31a0e04ab3SIan Lepore
3213a98c85SIan Lepore #include <sys/param.h>
3313a98c85SIan Lepore #include <sys/systm.h>
3430b72b68SRuslan Bukin #include <sys/devmap.h>
3513a98c85SIan Lepore #include <vm/vm.h>
3613a98c85SIan Lepore #include <vm/vm_extern.h>
3713a98c85SIan Lepore #include <vm/pmap.h>
3867cca85eSAndrew Turner #include <machine/vmparam.h>
3913a98c85SIan Lepore
40d90e3482SAndrew Turner #ifdef __arm__
41d90e3482SAndrew Turner #include <machine/pte.h>
42d90e3482SAndrew Turner #endif
43d90e3482SAndrew Turner
44a5d9ecfaSAndrew Turner #ifdef __HAVE_STATIC_DEVMAP
45789aed31SAndrew Turner #define DEVMAP_PADDR_NOTFOUND ((vm_paddr_t)(-1))
46789aed31SAndrew Turner
4730b72b68SRuslan Bukin static const struct devmap_entry *devmap_table;
48b4df095cSIan Lepore static boolean_t devmap_bootstrap_done = false;
49b4df095cSIan Lepore
50b4df095cSIan Lepore /*
51b4df095cSIan Lepore * The allocated-kva (akva) devmap table and metadata. Platforms can call
5230b72b68SRuslan Bukin * devmap_add_entry() to add static device mappings to this table using
53b4df095cSIan Lepore * automatically allocated virtual addresses carved out of the top of kva space.
541d954438SKristof Provost * Allocation begins immediately below the max kernel virtual address.
55b4df095cSIan Lepore */
56b4df095cSIan Lepore #define AKVA_DEVMAP_MAX_ENTRIES 32
5730b72b68SRuslan Bukin static struct devmap_entry akva_devmap_entries[AKVA_DEVMAP_MAX_ENTRIES];
58b4df095cSIan Lepore static u_int akva_devmap_idx;
59a5d9ecfaSAndrew Turner #endif
6030b72b68SRuslan Bukin static vm_offset_t akva_devmap_vaddr = DEVMAP_MAX_VADDR;
6167cca85eSAndrew Turner
62ca20f8ecSRuslan Bukin #if defined(__aarch64__) || defined(__riscv)
6367cca85eSAndrew Turner extern int early_boot;
6467cca85eSAndrew Turner #endif
65b4df095cSIan Lepore
66a5d9ecfaSAndrew Turner #ifdef __HAVE_STATIC_DEVMAP
67b4df095cSIan Lepore /*
68a0e04ab3SIan Lepore * Print the contents of the static mapping table using the provided printf-like
69a0e04ab3SIan Lepore * output function (which will be either printf or db_printf).
70a0e04ab3SIan Lepore */
71a0e04ab3SIan Lepore static void
devmap_dump_table(int (* prfunc)(const char *,...))72a0e04ab3SIan Lepore devmap_dump_table(int (*prfunc)(const char *, ...))
73a0e04ab3SIan Lepore {
7430b72b68SRuslan Bukin const struct devmap_entry *pd;
75a0e04ab3SIan Lepore
76a0e04ab3SIan Lepore if (devmap_table == NULL || devmap_table[0].pd_size == 0) {
77a0e04ab3SIan Lepore prfunc("No static device mappings.\n");
78a0e04ab3SIan Lepore return;
79a0e04ab3SIan Lepore }
80a0e04ab3SIan Lepore
81a0e04ab3SIan Lepore prfunc("Static device mappings:\n");
82a0e04ab3SIan Lepore for (pd = devmap_table; pd->pd_size != 0; ++pd) {
831b6dd6d7SPhilip Paeps prfunc(" 0x%08jx - 0x%08jx mapped at VA 0x%08jx\n",
849976b42bSThomas Skibo (uintmax_t)pd->pd_pa,
859976b42bSThomas Skibo (uintmax_t)(pd->pd_pa + pd->pd_size - 1),
869976b42bSThomas Skibo (uintmax_t)pd->pd_va);
87a0e04ab3SIan Lepore }
88a0e04ab3SIan Lepore }
89a0e04ab3SIan Lepore
90a0e04ab3SIan Lepore /*
91a0e04ab3SIan Lepore * Print the contents of the static mapping table. Used for bootverbose.
92a0e04ab3SIan Lepore */
93a0e04ab3SIan Lepore void
devmap_print_table(void)948e9ca137SAndrew Turner devmap_print_table(void)
95a0e04ab3SIan Lepore {
96a0e04ab3SIan Lepore devmap_dump_table(printf);
97a0e04ab3SIan Lepore }
98a0e04ab3SIan Lepore
99a0e04ab3SIan Lepore /*
100b4df095cSIan Lepore * Return the "last" kva address used by the registered devmap table. It's
101b4df095cSIan Lepore * actually the lowest address used by the static mappings, i.e., the address of
102b4df095cSIan Lepore * the first unusable byte of KVA.
103b4df095cSIan Lepore */
104b4df095cSIan Lepore vm_offset_t
devmap_lastaddr(void)1058e9ca137SAndrew Turner devmap_lastaddr(void)
106b4df095cSIan Lepore {
10730b72b68SRuslan Bukin const struct devmap_entry *pd;
1080f7191e8SIan Lepore vm_offset_t lowaddr;
109b4df095cSIan Lepore
110b4df095cSIan Lepore if (akva_devmap_idx > 0)
111b4df095cSIan Lepore return (akva_devmap_vaddr);
112b4df095cSIan Lepore
11330b72b68SRuslan Bukin lowaddr = DEVMAP_MAX_VADDR;
11462a70ef6SIan Lepore for (pd = devmap_table; pd != NULL && pd->pd_size != 0; ++pd) {
115b4df095cSIan Lepore if (lowaddr > pd->pd_va)
116b4df095cSIan Lepore lowaddr = pd->pd_va;
117b4df095cSIan Lepore }
118b4df095cSIan Lepore
119b4df095cSIan Lepore return (lowaddr);
120b4df095cSIan Lepore }
121b4df095cSIan Lepore
122b4df095cSIan Lepore /*
123b4df095cSIan Lepore * Add an entry to the internal "akva" static devmap table using the given
124b4df095cSIan Lepore * physical address and size and a virtual address allocated from the top of
125b4df095cSIan Lepore * kva. This automatically registers the akva table on the first call, so all a
126b4df095cSIan Lepore * platform has to do is call this routine to install as many mappings as it
1271d954438SKristof Provost * needs and when the platform-specific init function calls devmap_bootstrap()
1281d954438SKristof Provost * it will pick up all the entries in the akva table automatically.
129b4df095cSIan Lepore */
130b4df095cSIan Lepore void
devmap_add_entry(vm_paddr_t pa,vm_size_t sz)13130b72b68SRuslan Bukin devmap_add_entry(vm_paddr_t pa, vm_size_t sz)
132b4df095cSIan Lepore {
13330b72b68SRuslan Bukin struct devmap_entry *m;
134b4df095cSIan Lepore
135b4df095cSIan Lepore if (devmap_bootstrap_done)
13630b72b68SRuslan Bukin panic("devmap_add_entry() after devmap_bootstrap()");
137b4df095cSIan Lepore
138b4df095cSIan Lepore if (akva_devmap_idx == (AKVA_DEVMAP_MAX_ENTRIES - 1))
1390f7191e8SIan Lepore panic("AKVA_DEVMAP_MAX_ENTRIES is too small");
140b4df095cSIan Lepore
141b4df095cSIan Lepore if (akva_devmap_idx == 0)
14230b72b68SRuslan Bukin devmap_register_table(akva_devmap_entries);
143b4df095cSIan Lepore
1441d954438SKristof Provost /* Allocate virtual address space from the top of kva downwards. */
1451d954438SKristof Provost /*
1461d954438SKristof Provost * If the range being mapped is aligned and sized to 1MB boundaries then
1471d954438SKristof Provost * also align the virtual address to the next-lower 1MB boundary so that
1481d954438SKristof Provost * we end with a nice efficient section mapping.
1491d954438SKristof Provost */
150d90e3482SAndrew Turner if ((pa & L1_S_OFFSET) == 0 && (sz & L1_S_OFFSET) == 0) {
151b4df095cSIan Lepore akva_devmap_vaddr = trunc_1mpage(akva_devmap_vaddr - sz);
152a5d9ecfaSAndrew Turner } else {
153b4df095cSIan Lepore akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - sz);
154b4df095cSIan Lepore }
155b4df095cSIan Lepore m = &akva_devmap_entries[akva_devmap_idx++];
156b4df095cSIan Lepore m->pd_va = akva_devmap_vaddr;
157b4df095cSIan Lepore m->pd_pa = pa;
158b4df095cSIan Lepore m->pd_size = sz;
159b4df095cSIan Lepore }
16013a98c85SIan Lepore
16113a98c85SIan Lepore /*
16230b72b68SRuslan Bukin * Register the given table as the one to use in devmap_bootstrap().
16313a98c85SIan Lepore */
16413a98c85SIan Lepore void
devmap_register_table(const struct devmap_entry * table)16530b72b68SRuslan Bukin devmap_register_table(const struct devmap_entry *table)
16613a98c85SIan Lepore {
16713a98c85SIan Lepore
16813a98c85SIan Lepore devmap_table = table;
1693110e7eeSIan Lepore }
1703110e7eeSIan Lepore
1713110e7eeSIan Lepore /*
1723110e7eeSIan Lepore * Map all of the static regions in the devmap table, and remember the devmap
1733110e7eeSIan Lepore * table so the mapdev, ptov, and vtop functions can do lookups later.
1743110e7eeSIan Lepore */
1753110e7eeSIan Lepore void
devmap_bootstrap(void)1765df74441SMitchell Horne devmap_bootstrap(void)
1773110e7eeSIan Lepore {
17830b72b68SRuslan Bukin const struct devmap_entry *pd;
1793110e7eeSIan Lepore
18062a70ef6SIan Lepore devmap_bootstrap_done = true;
18162a70ef6SIan Lepore
1823110e7eeSIan Lepore /*
1835df74441SMitchell Horne * If a table was previously registered, use it. Otherwise, no work to
1845df74441SMitchell Horne * do.
1853110e7eeSIan Lepore */
1865df74441SMitchell Horne if (devmap_table == NULL)
18762a70ef6SIan Lepore return;
18813a98c85SIan Lepore
18913a98c85SIan Lepore for (pd = devmap_table; pd->pd_size != 0; ++pd) {
190677ba850SSvatopluk Kraus pmap_preboot_map_attr(pd->pd_pa, pd->pd_va, pd->pd_size,
1911413a3abSSvatopluk Kraus VM_PROT_READ | VM_PROT_WRITE, VM_MEMATTR_DEVICE);
19213a98c85SIan Lepore }
19313a98c85SIan Lepore }
19413a98c85SIan Lepore
19513a98c85SIan Lepore /*
19613a98c85SIan Lepore * Look up the given physical address in the static mapping data and return the
19713a98c85SIan Lepore * corresponding virtual address, or NULL if not found.
19813a98c85SIan Lepore */
199789aed31SAndrew Turner static void *
devmap_ptov(vm_paddr_t pa,vm_size_t size)20030b72b68SRuslan Bukin devmap_ptov(vm_paddr_t pa, vm_size_t size)
20113a98c85SIan Lepore {
20230b72b68SRuslan Bukin const struct devmap_entry *pd;
20313a98c85SIan Lepore
20413a98c85SIan Lepore if (devmap_table == NULL)
20513a98c85SIan Lepore return (NULL);
20613a98c85SIan Lepore
20713a98c85SIan Lepore for (pd = devmap_table; pd->pd_size != 0; ++pd) {
20813a98c85SIan Lepore if (pa >= pd->pd_pa && pa + size <= pd->pd_pa + pd->pd_size)
20913a98c85SIan Lepore return ((void *)(pd->pd_va + (pa - pd->pd_pa)));
21013a98c85SIan Lepore }
21113a98c85SIan Lepore
21213a98c85SIan Lepore return (NULL);
21313a98c85SIan Lepore }
21413a98c85SIan Lepore
21513a98c85SIan Lepore /*
21613a98c85SIan Lepore * Look up the given virtual address in the static mapping data and return the
21713a98c85SIan Lepore * corresponding physical address, or DEVMAP_PADDR_NOTFOUND if not found.
21813a98c85SIan Lepore */
219789aed31SAndrew Turner static vm_paddr_t
devmap_vtop(void * vpva,vm_size_t size)22030b72b68SRuslan Bukin devmap_vtop(void * vpva, vm_size_t size)
22113a98c85SIan Lepore {
22230b72b68SRuslan Bukin const struct devmap_entry *pd;
22313a98c85SIan Lepore vm_offset_t va;
22413a98c85SIan Lepore
22513a98c85SIan Lepore if (devmap_table == NULL)
22613a98c85SIan Lepore return (DEVMAP_PADDR_NOTFOUND);
22713a98c85SIan Lepore
22813a98c85SIan Lepore va = (vm_offset_t)vpva;
22913a98c85SIan Lepore for (pd = devmap_table; pd->pd_size != 0; ++pd) {
23013a98c85SIan Lepore if (va >= pd->pd_va && va + size <= pd->pd_va + pd->pd_size)
23113a98c85SIan Lepore return ((vm_paddr_t)(pd->pd_pa + (va - pd->pd_va)));
23213a98c85SIan Lepore }
23313a98c85SIan Lepore
23413a98c85SIan Lepore return (DEVMAP_PADDR_NOTFOUND);
23513a98c85SIan Lepore }
236a5d9ecfaSAndrew Turner #endif
23713a98c85SIan Lepore
23813a98c85SIan Lepore /*
23913a98c85SIan Lepore * Map a set of physical memory pages into the kernel virtual address space.
240c18fc3ffSIan Lepore * Return a pointer to where it is mapped.
241c18fc3ffSIan Lepore *
242c18fc3ffSIan Lepore * This uses a pre-established static mapping if one exists for the requested
243c18fc3ffSIan Lepore * range, otherwise it allocates kva space and maps the physical pages into it.
244c18fc3ffSIan Lepore *
245c18fc3ffSIan Lepore * This routine is intended to be used for mapping device memory, NOT real
2461413a3abSSvatopluk Kraus * memory; the mapping type is inherently VM_MEMATTR_DEVICE in
2471413a3abSSvatopluk Kraus * pmap_kenter_device().
24813a98c85SIan Lepore */
24913a98c85SIan Lepore void *
pmap_mapdev(vm_paddr_t pa,vm_size_t size)250ea8f128cSJohn Baldwin pmap_mapdev(vm_paddr_t pa, vm_size_t size)
25113a98c85SIan Lepore {
252ebeeeae6SAndrew Turner return (pmap_mapdev_attr(pa, size, VM_MEMATTR_DEVICE));
25313a98c85SIan Lepore }
25413a98c85SIan Lepore
25578442297SEmmanuel Vadot void *
pmap_mapdev_attr(vm_paddr_t pa,vm_size_t size,vm_memattr_t ma)256ea8f128cSJohn Baldwin pmap_mapdev_attr(vm_paddr_t pa, vm_size_t size, vm_memattr_t ma)
25778442297SEmmanuel Vadot {
25878442297SEmmanuel Vadot vm_offset_t va, offset;
259a5d9ecfaSAndrew Turner #ifdef __HAVE_STATIC_DEVMAP
26078442297SEmmanuel Vadot void * rva;
26178442297SEmmanuel Vadot
262ebeeeae6SAndrew Turner /*
263ebeeeae6SAndrew Turner * First look in the static mapping table. These are all mapped
264ebeeeae6SAndrew Turner * as device memory, so only use the devmap for VM_MEMATTR_DEVICE.
265ebeeeae6SAndrew Turner */
266ebeeeae6SAndrew Turner if ((rva = devmap_ptov(pa, size)) != NULL) {
267ebeeeae6SAndrew Turner KASSERT(ma == VM_MEMATTR_DEVICE,
268ebeeeae6SAndrew Turner ("%s: Non-device mapping for pa %jx (type %x)", __func__,
269ebeeeae6SAndrew Turner (uintmax_t)pa, ma));
27078442297SEmmanuel Vadot return (rva);
271ebeeeae6SAndrew Turner }
272a5d9ecfaSAndrew Turner #endif
27378442297SEmmanuel Vadot
27478442297SEmmanuel Vadot offset = pa & PAGE_MASK;
27578442297SEmmanuel Vadot pa = trunc_page(pa);
27678442297SEmmanuel Vadot size = round_page(size + offset);
27778442297SEmmanuel Vadot
278ebeeeae6SAndrew Turner #ifdef PMAP_MAPDEV_EARLY_SIZE
27978442297SEmmanuel Vadot if (early_boot) {
28078442297SEmmanuel Vadot akva_devmap_vaddr = trunc_page(akva_devmap_vaddr - size);
28178442297SEmmanuel Vadot va = akva_devmap_vaddr;
28266886d9dSAndrew Turner KASSERT(va >= (VM_MAX_KERNEL_ADDRESS - PMAP_MAPDEV_EARLY_SIZE),
28366886d9dSAndrew Turner ("%s: Too many early devmap mappings", __func__));
28478442297SEmmanuel Vadot } else
285ebeeeae6SAndrew Turner #endif
286e0388a90SAlan Cox #ifdef __aarch64__
287e0388a90SAlan Cox if (size >= L2_SIZE && (pa & L2_OFFSET) == 0)
288e0388a90SAlan Cox va = kva_alloc_aligned(size, L2_SIZE);
289e0388a90SAlan Cox else if (size >= L3C_SIZE && (pa & L3C_OFFSET) == 0)
290e0388a90SAlan Cox va = kva_alloc_aligned(size, L3C_SIZE);
291e0388a90SAlan Cox else
292e0388a90SAlan Cox #endif
29378442297SEmmanuel Vadot va = kva_alloc(size);
29478442297SEmmanuel Vadot if (!va)
29578442297SEmmanuel Vadot panic("pmap_mapdev: Couldn't alloc kernel virtual memory");
29678442297SEmmanuel Vadot
29778442297SEmmanuel Vadot pmap_kenter(va, size, pa, ma);
29878442297SEmmanuel Vadot
29978442297SEmmanuel Vadot return ((void *)(va + offset));
30078442297SEmmanuel Vadot }
30178442297SEmmanuel Vadot
30213a98c85SIan Lepore /*
30313a98c85SIan Lepore * Unmap device memory and free the kva space.
30413a98c85SIan Lepore */
30513a98c85SIan Lepore void
pmap_unmapdev(void * p,vm_size_t size)3067ae99f80SJohn Baldwin pmap_unmapdev(void *p, vm_size_t size)
30713a98c85SIan Lepore {
3087ae99f80SJohn Baldwin vm_offset_t offset, va;
30913a98c85SIan Lepore
310a5d9ecfaSAndrew Turner #ifdef __HAVE_STATIC_DEVMAP
311c18fc3ffSIan Lepore /* Nothing to do if we find the mapping in the static table. */
3127ae99f80SJohn Baldwin if (devmap_vtop(p, size) != DEVMAP_PADDR_NOTFOUND)
313c18fc3ffSIan Lepore return;
314a5d9ecfaSAndrew Turner #endif
315c18fc3ffSIan Lepore
3167ae99f80SJohn Baldwin va = (vm_offset_t)p;
31713a98c85SIan Lepore offset = va & PAGE_MASK;
31813a98c85SIan Lepore va = trunc_page(va);
31913a98c85SIan Lepore size = round_page(size + offset);
32013a98c85SIan Lepore
3217669914fSIan Lepore pmap_kremove_device(va, size);
3227669914fSIan Lepore kva_free(va, size);
32313a98c85SIan Lepore }
32413a98c85SIan Lepore
325a0e04ab3SIan Lepore #ifdef DDB
326a5d9ecfaSAndrew Turner #ifdef __HAVE_STATIC_DEVMAP
327a0e04ab3SIan Lepore #include <ddb/ddb.h>
328a0e04ab3SIan Lepore
DB_SHOW_COMMAND_FLAGS(devmap,db_show_devmap,DB_CMD_MEMSAFE)329c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(devmap, db_show_devmap, DB_CMD_MEMSAFE)
330a0e04ab3SIan Lepore {
331a0e04ab3SIan Lepore devmap_dump_table(db_printf);
332a0e04ab3SIan Lepore }
333a0e04ab3SIan Lepore
334a5d9ecfaSAndrew Turner #endif
335a0e04ab3SIan Lepore #endif /* DDB */
336