1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Isilon Systems, Inc.
5  * Copyright (c) 2016 Matthew Macy (mmacy@mattmacy.io)
6  * Copyright (c) 2017 Mellanox Technologies, Ltd.
7  * Copyright (c) 2021 Vladimir Kondratyev <wulf@FreeBSD.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUXKPI_LINUX_HIGHMEM_H_
33 #define _LINUXKPI_LINUX_HIGHMEM_H_
34 
35 #include <sys/types.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/proc.h>
39 #include <sys/sched.h>
40 #include <sys/sf_buf.h>
41 
42 #include <vm/vm.h>
43 #include <vm/vm_page.h>
44 #include <vm/pmap.h>
45 
46 #include <linux/page.h>
47 
48 #define	PageHighMem(p)		(0)
49 
50 static inline vm_page_t
51 kmap_to_page(void *addr)
52 {
53 	return (virt_to_page(addr));
54 }
55 
56 static inline void *
57 kmap(vm_page_t page)
58 {
59 	struct sf_buf *sf;
60 
61 	if (PMAP_HAS_DMAP) {
62 		return ((void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(page)));
63 	} else {
64 		sched_pin();
65 		sf = sf_buf_alloc(page, SFB_NOWAIT | SFB_CPUPRIVATE);
66 		if (sf == NULL) {
67 			sched_unpin();
68 			return (NULL);
69 		}
70 		return ((void *)sf_buf_kva(sf));
71 	}
72 }
73 
74 static inline void *
75 kmap_atomic_prot(vm_page_t page, pgprot_t prot)
76 {
77 	vm_memattr_t attr = pgprot2cachemode(prot);
78 
79 	if (attr != VM_MEMATTR_DEFAULT) {
80 		vm_page_lock(page);
81 		page->flags |= PG_FICTITIOUS;
82 		vm_page_unlock(page);
83 		pmap_page_set_memattr(page, attr);
84 	}
85 	return (kmap(page));
86 }
87 
88 static inline void *
89 kmap_atomic(vm_page_t page)
90 {
91 	return (kmap_atomic_prot(page, VM_PROT_ALL));
92 }
93 
94 static inline void
95 kunmap(vm_page_t page)
96 {
97 	struct sf_buf *sf;
98 
99 	if (!PMAP_HAS_DMAP) {
100 		/* lookup SF buffer in list */
101 		sf = sf_buf_alloc(page, SFB_NOWAIT | SFB_CPUPRIVATE);
102 
103 		/* double-free */
104 		sf_buf_free(sf);
105 		sf_buf_free(sf);
106 
107 		sched_unpin();
108 	}
109 }
110 
111 static inline void
112 kunmap_atomic(void *vaddr)
113 {
114 	if (!PMAP_HAS_DMAP)
115 		kunmap(virt_to_page(vaddr));
116 }
117 
118 #endif	/* _LINUXKPI_LINUX_HIGHMEM_H_ */
119