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 54 return (virt_to_page(addr)); 55 } 56 57 static inline void * 58 kmap(vm_page_t page) 59 { 60 struct sf_buf *sf; 61 62 if (PMAP_HAS_DMAP) { 63 return ((void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(page))); 64 } else { 65 sched_pin(); 66 sf = sf_buf_alloc(page, SFB_NOWAIT | SFB_CPUPRIVATE); 67 if (sf == NULL) { 68 sched_unpin(); 69 return (NULL); 70 } 71 return ((void *)sf_buf_kva(sf)); 72 } 73 } 74 75 static inline void * 76 kmap_atomic_prot(vm_page_t page, pgprot_t prot) 77 { 78 vm_memattr_t attr = pgprot2cachemode(prot); 79 80 if (attr != VM_MEMATTR_DEFAULT) { 81 vm_page_lock(page); 82 page->flags |= PG_FICTITIOUS; 83 vm_page_unlock(page); 84 pmap_page_set_memattr(page, attr); 85 } 86 return (kmap(page)); 87 } 88 89 static inline void * 90 kmap_atomic(vm_page_t page) 91 { 92 93 return (kmap_atomic_prot(page, VM_PROT_ALL)); 94 } 95 96 static inline void * 97 kmap_local_page_prot(vm_page_t page, pgprot_t prot) 98 { 99 100 return (kmap_atomic_prot(page, prot)); 101 } 102 103 static inline void 104 kunmap(vm_page_t page) 105 { 106 struct sf_buf *sf; 107 108 if (!PMAP_HAS_DMAP) { 109 /* lookup SF buffer in list */ 110 sf = sf_buf_alloc(page, SFB_NOWAIT | SFB_CPUPRIVATE); 111 112 /* double-free */ 113 sf_buf_free(sf); 114 sf_buf_free(sf); 115 116 sched_unpin(); 117 } 118 } 119 120 static inline void 121 kunmap_atomic(void *vaddr) 122 { 123 124 if (!PMAP_HAS_DMAP) 125 kunmap(virt_to_page(vaddr)); 126 } 127 128 static inline void 129 kunmap_local(void *addr) 130 { 131 132 kunmap_atomic(addr); 133 } 134 135 #endif /* _LINUXKPI_LINUX_HIGHMEM_H_ */ 136