xref: /freebsd/sys/compat/linuxkpi/common/include/asm/set_memory.h (revision e63d20b70ee1dbee9b075f29de6f30cdcfe1abe1)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2016 Matt Macy (mmacy@nextbsd.org)
4  * Copyright (c) 2017 Mellanox Technologies, Ltd.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _LINUXKPI_ASM_SET_MEMORY_H_
30 #define	_LINUXKPI_ASM_SET_MEMORY_H_
31 
32 #include <linux/page.h>
33 
34 static inline int
35 set_memory_uc(unsigned long addr, int numpages)
36 {
37 	vm_size_t len;
38 
39 	len = (vm_size_t)numpages << PAGE_SHIFT;
40 	return (-pmap_change_attr(addr, len, VM_MEMATTR_UNCACHEABLE));
41 }
42 
43 static inline int
44 set_memory_wc(unsigned long addr, int numpages)
45 {
46 #ifdef VM_MEMATTR_WRITE_COMBINING
47 	vm_size_t len;
48 
49 	len = (vm_size_t)numpages << PAGE_SHIFT;
50 	return (-pmap_change_attr(addr, len, VM_MEMATTR_WRITE_COMBINING));
51 #else
52 	return (set_memory_uc(addr, numpages));
53 #endif
54 }
55 
56 static inline int
57 set_memory_wb(unsigned long addr, int numpages)
58 {
59 	vm_size_t len;
60 
61 	len = (vm_size_t)numpages << PAGE_SHIFT;
62 	return (-pmap_change_attr(addr, len, VM_MEMATTR_WRITE_BACK));
63 }
64 
65 static inline int
66 set_pages_uc(struct page *page, int numpages)
67 {
68 	KASSERT(numpages == 1, ("%s: numpages %d", __func__, numpages));
69 
70 	pmap_page_set_memattr(page, VM_MEMATTR_UNCACHEABLE);
71 	return (0);
72 }
73 
74 static inline int
75 set_pages_wc(struct page *page, int numpages)
76 {
77 	KASSERT(numpages == 1, ("%s: numpages %d", __func__, numpages));
78 
79 #ifdef VM_MEMATTR_WRITE_COMBINING
80 	pmap_page_set_memattr(page, VM_MEMATTR_WRITE_COMBINING);
81 #else
82 	return (set_pages_uc(page, numpages));
83 #endif
84 	return (0);
85 }
86 
87 static inline int
88 set_pages_wb(struct page *page, int numpages)
89 {
90 	KASSERT(numpages == 1, ("%s: numpages %d", __func__, numpages));
91 
92 	pmap_page_set_memattr(page, VM_MEMATTR_WRITE_BACK);
93 	return (0);
94 }
95 
96 static inline int
97 set_pages_array_wb(struct page **pages, int addrinarray)
98 {
99 	int i;
100 
101 	for (i = 0; i < addrinarray; i++)
102 		set_pages_wb(pages[i], 1);
103 	return (0);
104 }
105 
106 static inline int
107 set_pages_array_wc(struct page **pages, int addrinarray)
108 {
109 	int i;
110 
111 	for (i = 0; i < addrinarray; i++)
112 		set_pages_wc(pages[i], 1);
113 	return (0);
114 }
115 
116 static inline int
117 set_pages_array_uc(struct page **pages, int addrinarray)
118 {
119 	int i;
120 
121 	for (i = 0; i < addrinarray; i++)
122 		set_pages_uc(pages[i], 1);
123 	return (0);
124 }
125 
126 #endif	/* _LINUXKPI_ASM_SET_MEMORY_H_ */
127