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 * 52 kmap_to_page(void *addr) 53 { 54 55 return (virt_to_page(addr)); 56 } 57 58 static inline void * 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 * 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 page->flags |= PG_FICTITIOUS; 83 pmap_page_set_memattr(page, attr); 84 } 85 return (kmap(page)); 86 } 87 88 static inline void * 89 kmap_atomic(struct page *page) 90 { 91 92 return (kmap_atomic_prot(page, VM_PROT_ALL)); 93 } 94 95 static inline void * 96 kmap_local_page(struct page *page) 97 { 98 return (kmap(page)); 99 } 100 101 static inline void * 102 kmap_local_page_prot(struct page *page, pgprot_t prot) 103 { 104 105 return (kmap_atomic_prot(page, prot)); 106 } 107 108 static inline void 109 kunmap(struct page *page) 110 { 111 struct sf_buf *sf; 112 113 if (!PMAP_HAS_DMAP) { 114 /* lookup SF buffer in list */ 115 sf = sf_buf_alloc(page, SFB_NOWAIT | SFB_CPUPRIVATE); 116 117 /* double-free */ 118 sf_buf_free(sf); 119 sf_buf_free(sf); 120 121 sched_unpin(); 122 } 123 } 124 125 static inline void 126 kunmap_atomic(void *vaddr) 127 { 128 129 if (!PMAP_HAS_DMAP) 130 kunmap(virt_to_page(vaddr)); 131 } 132 133 static inline void 134 kunmap_local(void *addr) 135 { 136 137 kunmap_atomic(addr); 138 } 139 140 static inline void 141 memcpy_from_page(char *to, struct page *page, size_t offset, size_t len) 142 { 143 char *from; 144 145 KASSERT(offset + len <= PAGE_SIZE, 146 ("%s: memcpy from page %p to address %p: " 147 "offset+len (%zu+%zu) would go beyond page end", 148 __func__, page, to, offset, len)); 149 150 from = kmap_local_page(page); 151 memcpy(to, from + offset, len); 152 kunmap_local(from); 153 } 154 155 static inline void 156 memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len) 157 { 158 char *to; 159 160 KASSERT(offset + len <= PAGE_SIZE, 161 ("%s: memcpy from address %p to page %p: " 162 "offset+len (%zu+%zu) would go beyond page end", 163 __func__, from, page, offset, len)); 164 165 to = kmap_local_page(page); 166 memcpy(to + offset, from, len); 167 kunmap_local(to); 168 } 169 170 #endif /* _LINUXKPI_LINUX_HIGHMEM_H_ */ 171