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 	return (pmap_change_attr(addr, numpages, VM_MEMATTR_UNCACHEABLE));
38 }
39 
40 static inline int
41 set_memory_wc(unsigned long addr, int numpages)
42 {
43 #ifdef VM_MEMATTR_WRITE_COMBINING
44 	return (pmap_change_attr(addr, numpages, VM_MEMATTR_WRITE_COMBINING));
45 #else
46 	return (set_memory_uc(addr, numpages));
47 #endif
48 }
49 
50 static inline int
51 set_memory_wb(unsigned long addr, int numpages)
52 {
53 	return (pmap_change_attr(addr, numpages, VM_MEMATTR_WRITE_BACK));
54 }
55 
56 static inline int
57 set_pages_uc(struct page *page, int numpages)
58 {
59 	KASSERT(numpages == 1, ("%s: numpages %d", __func__, numpages));
60 
61 	pmap_page_set_memattr(page, VM_MEMATTR_UNCACHEABLE);
62 	return (0);
63 }
64 
65 static inline int
66 set_pages_wc(struct page *page, int numpages)
67 {
68 	KASSERT(numpages == 1, ("%s: numpages %d", __func__, numpages));
69 
70 #ifdef VM_MEMATTR_WRITE_COMBINING
71 	pmap_page_set_memattr(page, VM_MEMATTR_WRITE_COMBINING);
72 #else
73 	return (set_pages_uc(page, numpages));
74 #endif
75 	return (0);
76 }
77 
78 static inline int
79 set_pages_wb(struct page *page, int numpages)
80 {
81 	KASSERT(numpages == 1, ("%s: numpages %d", __func__, numpages));
82 
83 	pmap_page_set_memattr(page, VM_MEMATTR_WRITE_BACK);
84 	return (0);
85 }
86 
87 static inline int
88 set_pages_array_wb(struct page **pages, int addrinarray)
89 {
90 	int i;
91 
92 	for (i = 0; i < addrinarray; i++)
93 		set_pages_wb(pages[i], 1);
94 	return (0);
95 }
96 
97 static inline int
98 set_pages_array_wc(struct page **pages, int addrinarray)
99 {
100 	int i;
101 
102 	for (i = 0; i < addrinarray; i++)
103 		set_pages_wc(pages[i], 1);
104 	return (0);
105 }
106 
107 static inline int
108 set_pages_array_uc(struct page **pages, int addrinarray)
109 {
110 	int i;
111 
112 	for (i = 0; i < addrinarray; i++)
113 		set_pages_uc(pages[i], 1);
114 	return (0);
115 }
116 
117 #endif	/* _LINUXKPI_ASM_SET_MEMORY_H_ */
118