]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/include/asm/cacheflush.h
x86: Use new cache mode type in memtype related functions
[karo-tx-linux.git] / arch / x86 / include / asm / cacheflush.h
1 #ifndef _ASM_X86_CACHEFLUSH_H
2 #define _ASM_X86_CACHEFLUSH_H
3
4 /* Caches aren't brain-dead on the intel. */
5 #include <asm-generic/cacheflush.h>
6 #include <asm/special_insns.h>
7
8 #ifdef CONFIG_X86_PAT
9 /*
10  * X86 PAT uses page flags WC and Uncached together to keep track of
11  * memory type of pages that have backing page struct. X86 PAT supports 3
12  * different memory types, _PAGE_CACHE_MODE_WB, _PAGE_CACHE_MODE_WC and
13  * _PAGE_CACHE_MODE_UC_MINUS and fourth state where page's memory type has not
14  * been changed from its default (value of -1 used to denote this).
15  * Note we do not support _PAGE_CACHE_MODE_UC here.
16  */
17
18 #define _PGMT_DEFAULT           0
19 #define _PGMT_WC                (1UL << PG_arch_1)
20 #define _PGMT_UC_MINUS          (1UL << PG_uncached)
21 #define _PGMT_WB                (1UL << PG_uncached | 1UL << PG_arch_1)
22 #define _PGMT_MASK              (1UL << PG_uncached | 1UL << PG_arch_1)
23 #define _PGMT_CLEAR_MASK        (~_PGMT_MASK)
24
25 static inline enum page_cache_mode get_page_memtype(struct page *pg)
26 {
27         unsigned long pg_flags = pg->flags & _PGMT_MASK;
28
29         if (pg_flags == _PGMT_DEFAULT)
30                 return -1;
31         else if (pg_flags == _PGMT_WC)
32                 return _PAGE_CACHE_MODE_WC;
33         else if (pg_flags == _PGMT_UC_MINUS)
34                 return _PAGE_CACHE_MODE_UC_MINUS;
35         else
36                 return _PAGE_CACHE_MODE_WB;
37 }
38
39 static inline void set_page_memtype(struct page *pg,
40                                     enum page_cache_mode memtype)
41 {
42         unsigned long memtype_flags;
43         unsigned long old_flags;
44         unsigned long new_flags;
45
46         switch (memtype) {
47         case _PAGE_CACHE_MODE_WC:
48                 memtype_flags = _PGMT_WC;
49                 break;
50         case _PAGE_CACHE_MODE_UC_MINUS:
51                 memtype_flags = _PGMT_UC_MINUS;
52                 break;
53         case _PAGE_CACHE_MODE_WB:
54                 memtype_flags = _PGMT_WB;
55                 break;
56         default:
57                 memtype_flags = _PGMT_DEFAULT;
58                 break;
59         }
60
61         do {
62                 old_flags = pg->flags;
63                 new_flags = (old_flags & _PGMT_CLEAR_MASK) | memtype_flags;
64         } while (cmpxchg(&pg->flags, old_flags, new_flags) != old_flags);
65 }
66 #else
67 static inline enum page_cache_mode get_page_memtype(struct page *pg)
68 {
69         return -1;
70 }
71 static inline void set_page_memtype(struct page *pg,
72                                     enum page_cache_mode memtype)
73 {
74 }
75 #endif
76
77 /*
78  * The set_memory_* API can be used to change various attributes of a virtual
79  * address range. The attributes include:
80  * Cachability   : UnCached, WriteCombining, WriteBack
81  * Executability : eXeutable, NoteXecutable
82  * Read/Write    : ReadOnly, ReadWrite
83  * Presence      : NotPresent
84  *
85  * Within a category, the attributes are mutually exclusive.
86  *
87  * The implementation of this API will take care of various aspects that
88  * are associated with changing such attributes, such as:
89  * - Flushing TLBs
90  * - Flushing CPU caches
91  * - Making sure aliases of the memory behind the mapping don't violate
92  *   coherency rules as defined by the CPU in the system.
93  *
94  * What this API does not do:
95  * - Provide exclusion between various callers - including callers that
96  *   operation on other mappings of the same physical page
97  * - Restore default attributes when a page is freed
98  * - Guarantee that mappings other than the requested one are
99  *   in any state, other than that these do not violate rules for
100  *   the CPU you have. Do not depend on any effects on other mappings,
101  *   CPUs other than the one you have may have more relaxed rules.
102  * The caller is required to take care of these.
103  */
104
105 int _set_memory_uc(unsigned long addr, int numpages);
106 int _set_memory_wc(unsigned long addr, int numpages);
107 int _set_memory_wb(unsigned long addr, int numpages);
108 int set_memory_uc(unsigned long addr, int numpages);
109 int set_memory_wc(unsigned long addr, int numpages);
110 int set_memory_wb(unsigned long addr, int numpages);
111 int set_memory_x(unsigned long addr, int numpages);
112 int set_memory_nx(unsigned long addr, int numpages);
113 int set_memory_ro(unsigned long addr, int numpages);
114 int set_memory_rw(unsigned long addr, int numpages);
115 int set_memory_np(unsigned long addr, int numpages);
116 int set_memory_4k(unsigned long addr, int numpages);
117
118 int set_memory_array_uc(unsigned long *addr, int addrinarray);
119 int set_memory_array_wc(unsigned long *addr, int addrinarray);
120 int set_memory_array_wb(unsigned long *addr, int addrinarray);
121
122 int set_pages_array_uc(struct page **pages, int addrinarray);
123 int set_pages_array_wc(struct page **pages, int addrinarray);
124 int set_pages_array_wb(struct page **pages, int addrinarray);
125
126 /*
127  * For legacy compatibility with the old APIs, a few functions
128  * are provided that work on a "struct page".
129  * These functions operate ONLY on the 1:1 kernel mapping of the
130  * memory that the struct page represents, and internally just
131  * call the set_memory_* function. See the description of the
132  * set_memory_* function for more details on conventions.
133  *
134  * These APIs should be considered *deprecated* and are likely going to
135  * be removed in the future.
136  * The reason for this is the implicit operation on the 1:1 mapping only,
137  * making this not a generally useful API.
138  *
139  * Specifically, many users of the old APIs had a virtual address,
140  * called virt_to_page() or vmalloc_to_page() on that address to
141  * get a struct page* that the old API required.
142  * To convert these cases, use set_memory_*() on the original
143  * virtual address, do not use these functions.
144  */
145
146 int set_pages_uc(struct page *page, int numpages);
147 int set_pages_wb(struct page *page, int numpages);
148 int set_pages_x(struct page *page, int numpages);
149 int set_pages_nx(struct page *page, int numpages);
150 int set_pages_ro(struct page *page, int numpages);
151 int set_pages_rw(struct page *page, int numpages);
152
153
154 void clflush_cache_range(void *addr, unsigned int size);
155
156 #ifdef CONFIG_DEBUG_RODATA
157 void mark_rodata_ro(void);
158 extern const int rodata_test_data;
159 extern int kernel_set_to_readonly;
160 void set_kernel_text_rw(void);
161 void set_kernel_text_ro(void);
162 #else
163 static inline void set_kernel_text_rw(void) { }
164 static inline void set_kernel_text_ro(void) { }
165 #endif
166
167 #ifdef CONFIG_DEBUG_RODATA_TEST
168 int rodata_test(void);
169 #else
170 static inline int rodata_test(void)
171 {
172         return 0;
173 }
174 #endif
175
176 #endif /* _ASM_X86_CACHEFLUSH_H */