xref: /freebsd/sys/compat/linuxkpi/common/include/linux/highmem.h (revision d9a2c2e250ae561fc2ff6c12dda53f70fb8c76b6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/mm.h>
47 #include <linux/page.h>
48 
49 #define	PageHighMem(p)		(0)
50 
51 static inline struct page *
kmap_to_page(void * addr)52 kmap_to_page(void *addr)
53 {
54 
55 	return (virt_to_page(addr));
56 }
57 
58 static inline void *
kmap(struct page * page)59 kmap(struct page *page)
60 {
61 	struct sf_buf *sf;
62 
63 	if (PMAP_HAS_DMAP) {
64 		return ((void *)PHYS_TO_DMAP(page_to_phys(page)));
65 	} else {
66 		sched_pin();
67 		sf = sf_buf_alloc(page, SFB_NOWAIT | SFB_CPUPRIVATE);
68 		if (sf == NULL) {
69 			sched_unpin();
70 			return (NULL);
71 		}
72 		return ((void *)sf_buf_kva(sf));
73 	}
74 }
75 
76 static inline void *
kmap_atomic_prot(struct page * page,pgprot_t prot)77 kmap_atomic_prot(struct page *page, pgprot_t prot)
78 {
79 	vm_memattr_t attr = pgprot2cachemode(prot);
80 
81 	if (attr != VM_MEMATTR_DEFAULT) {
82 		vm_page_lock(page);
83 		page->flags |= PG_FICTITIOUS;
84 		vm_page_unlock(page);
85 		pmap_page_set_memattr(page, attr);
86 	}
87 	return (kmap(page));
88 }
89 
90 static inline void *
kmap_atomic(struct page * page)91 kmap_atomic(struct page *page)
92 {
93 
94 	return (kmap_atomic_prot(page, VM_PROT_ALL));
95 }
96 
97 static inline void *
kmap_local_page(struct page * page)98 kmap_local_page(struct page *page)
99 {
100 	return (kmap(page));
101 }
102 
103 static inline void *
kmap_local_page_prot(struct page * page,pgprot_t prot)104 kmap_local_page_prot(struct page *page, pgprot_t prot)
105 {
106 
107 	return (kmap_atomic_prot(page, prot));
108 }
109 
110 static inline void
kunmap(struct page * page)111 kunmap(struct page *page)
112 {
113 	struct sf_buf *sf;
114 
115 	if (!PMAP_HAS_DMAP) {
116 		/* lookup SF buffer in list */
117 		sf = sf_buf_alloc(page, SFB_NOWAIT | SFB_CPUPRIVATE);
118 
119 		/* double-free */
120 		sf_buf_free(sf);
121 		sf_buf_free(sf);
122 
123 		sched_unpin();
124 	}
125 }
126 
127 static inline void
kunmap_atomic(void * vaddr)128 kunmap_atomic(void *vaddr)
129 {
130 
131 	if (!PMAP_HAS_DMAP)
132 		kunmap(virt_to_page(vaddr));
133 }
134 
135 static inline void
kunmap_local(void * addr)136 kunmap_local(void *addr)
137 {
138 
139 	kunmap_atomic(addr);
140 }
141 
142 static inline void
memcpy_from_page(char * to,struct page * page,size_t offset,size_t len)143 memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
144 {
145 	char *from;
146 
147 	KASSERT(offset + len <= PAGE_SIZE,
148 	    ("%s: memcpy from page %p to address %p: "
149 	     "offset+len (%zu+%zu) would go beyond page end",
150 	     __func__, page, to, offset, len));
151 
152 	from = kmap_local_page(page);
153 	memcpy(to, from + offset, len);
154 	kunmap_local(from);
155 }
156 
157 static inline void
memcpy_to_page(struct page * page,size_t offset,const char * from,size_t len)158 memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
159 {
160 	char *to;
161 
162 	KASSERT(offset + len <= PAGE_SIZE,
163 	    ("%s: memcpy from address %p to page %p: "
164 	     "offset+len (%zu+%zu) would go beyond page end",
165 	     __func__, from, page, offset, len));
166 
167 	to = kmap_local_page(page);
168 	memcpy(to + offset, from, len);
169 	kunmap_local(to);
170 }
171 
172 #endif	/* _LINUXKPI_LINUX_HIGHMEM_H_ */
173