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 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 * 91 kmap_atomic(struct page *page) 92 { 93 94 return (kmap_atomic_prot(page, VM_PROT_ALL)); 95 } 96 97 static inline void * 98 kmap_local_page(struct page *page) 99 { 100 return (kmap(page)); 101 } 102 103 static inline void * 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 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 128 kunmap_atomic(void *vaddr) 129 { 130 131 if (!PMAP_HAS_DMAP) 132 kunmap(virt_to_page(vaddr)); 133 } 134 135 static inline void 136 kunmap_local(void *addr) 137 { 138 139 kunmap_atomic(addr); 140 } 141 142 #endif /* _LINUXKPI_LINUX_HIGHMEM_H_ */ 143