blob: 62f084478f7e08448050cac33152f8f62bed8b72 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_CACHEFLUSH_H
2#define _ASM_X86_CACHEFLUSH_H
Thomas Gleixnerb2bba722007-10-15 23:28:20 +02003
Thomas Gleixnerb2bba722007-10-15 23:28:20 +02004/* Caches aren't brain-dead on the intel. */
Akinobu Mitacc67ba632011-01-20 20:32:14 +09005#include <asm-generic/cacheflush.h>
Thomas Gleixnerb2bba722007-10-15 23:28:20 +02006
Venkatesh Pallipadif5841742009-07-10 09:57:38 -07007#ifdef CONFIG_X86_PAT
8/*
9 * X86 PAT uses page flags WC and Uncached together to keep track of
10 * memory type of pages that have backing page struct. X86 PAT supports 3
11 * different memory types, _PAGE_CACHE_WB, _PAGE_CACHE_WC and
12 * _PAGE_CACHE_UC_MINUS and fourth state where page's memory type has not
13 * been changed from its default (value of -1 used to denote this).
14 * Note we do not support _PAGE_CACHE_UC here.
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070015 */
Robin Holt1f9cc3c2010-04-23 10:36:22 -050016
17#define _PGMT_DEFAULT 0
18#define _PGMT_WC (1UL << PG_arch_1)
19#define _PGMT_UC_MINUS (1UL << PG_uncached)
20#define _PGMT_WB (1UL << PG_uncached | 1UL << PG_arch_1)
21#define _PGMT_MASK (1UL << PG_uncached | 1UL << PG_arch_1)
22#define _PGMT_CLEAR_MASK (~_PGMT_MASK)
23
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070024static inline unsigned long get_page_memtype(struct page *pg)
25{
Robin Holt1f9cc3c2010-04-23 10:36:22 -050026 unsigned long pg_flags = pg->flags & _PGMT_MASK;
27
28 if (pg_flags == _PGMT_DEFAULT)
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070029 return -1;
Robin Holt1f9cc3c2010-04-23 10:36:22 -050030 else if (pg_flags == _PGMT_WC)
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070031 return _PAGE_CACHE_WC;
Robin Holt1f9cc3c2010-04-23 10:36:22 -050032 else if (pg_flags == _PGMT_UC_MINUS)
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070033 return _PAGE_CACHE_UC_MINUS;
34 else
35 return _PAGE_CACHE_WB;
36}
37
38static inline void set_page_memtype(struct page *pg, unsigned long memtype)
39{
Robin Holt1f9cc3c2010-04-23 10:36:22 -050040 unsigned long memtype_flags = _PGMT_DEFAULT;
41 unsigned long old_flags;
42 unsigned long new_flags;
43
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070044 switch (memtype) {
45 case _PAGE_CACHE_WC:
Robin Holt1f9cc3c2010-04-23 10:36:22 -050046 memtype_flags = _PGMT_WC;
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070047 break;
48 case _PAGE_CACHE_UC_MINUS:
Robin Holt1f9cc3c2010-04-23 10:36:22 -050049 memtype_flags = _PGMT_UC_MINUS;
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070050 break;
51 case _PAGE_CACHE_WB:
Robin Holt1f9cc3c2010-04-23 10:36:22 -050052 memtype_flags = _PGMT_WB;
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070053 break;
54 }
Robin Holt1f9cc3c2010-04-23 10:36:22 -050055
56 do {
57 old_flags = pg->flags;
58 new_flags = (old_flags & _PGMT_CLEAR_MASK) | memtype_flags;
59 } while (cmpxchg(&pg->flags, old_flags, new_flags) != old_flags);
Venkatesh Pallipadif5841742009-07-10 09:57:38 -070060}
61#else
62static inline unsigned long get_page_memtype(struct page *pg) { return -1; }
63static inline void set_page_memtype(struct page *pg, unsigned long memtype) { }
64#endif
Arjan van de Ven75cbade2008-01-30 13:34:06 +010065
Arjan van de Ven7219beb2008-04-17 17:41:31 +020066/*
67 * The set_memory_* API can be used to change various attributes of a virtual
68 * address range. The attributes include:
69 * Cachability : UnCached, WriteCombining, WriteBack
70 * Executability : eXeutable, NoteXecutable
71 * Read/Write : ReadOnly, ReadWrite
72 * Presence : NotPresent
73 *
74 * Within a catagory, the attributes are mutually exclusive.
75 *
76 * The implementation of this API will take care of various aspects that
77 * are associated with changing such attributes, such as:
78 * - Flushing TLBs
79 * - Flushing CPU caches
80 * - Making sure aliases of the memory behind the mapping don't violate
81 * coherency rules as defined by the CPU in the system.
82 *
83 * What this API does not do:
84 * - Provide exclusion between various callers - including callers that
85 * operation on other mappings of the same physical page
86 * - Restore default attributes when a page is freed
87 * - Guarantee that mappings other than the requested one are
88 * in any state, other than that these do not violate rules for
89 * the CPU you have. Do not depend on any effects on other mappings,
90 * CPUs other than the one you have may have more relaxed rules.
91 * The caller is required to take care of these.
92 */
Arjan van de Ven75cbade2008-01-30 13:34:06 +010093
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -070094int _set_memory_uc(unsigned long addr, int numpages);
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -070095int _set_memory_wc(unsigned long addr, int numpages);
venkatesh.pallipadi@intel.com12193332008-03-18 17:00:18 -070096int _set_memory_wb(unsigned long addr, int numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +010097int set_memory_uc(unsigned long addr, int numpages);
venkatesh.pallipadi@intel.comef354af2008-03-18 17:00:23 -070098int set_memory_wc(unsigned long addr, int numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +010099int set_memory_wb(unsigned long addr, int numpages);
100int set_memory_x(unsigned long addr, int numpages);
101int set_memory_nx(unsigned long addr, int numpages);
102int set_memory_ro(unsigned long addr, int numpages);
103int set_memory_rw(unsigned long addr, int numpages);
Ingo Molnarf62d0f02008-01-30 13:34:07 +0100104int set_memory_np(unsigned long addr, int numpages);
Andi Kleenc9caa022008-03-12 03:53:29 +0100105int set_memory_4k(unsigned long addr, int numpages);
Arjan van de Ven75cbade2008-01-30 13:34:06 +0100106
Shaohua Lid75586a2008-08-21 10:46:06 +0800107int set_memory_array_uc(unsigned long *addr, int addrinarray);
Pauli Nieminen4f646252010-04-01 12:45:01 +0000108int set_memory_array_wc(unsigned long *addr, int addrinarray);
Shaohua Lid75586a2008-08-21 10:46:06 +0800109int set_memory_array_wb(unsigned long *addr, int addrinarray);
110
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -0700111int set_pages_array_uc(struct page **pages, int addrinarray);
Pauli Nieminen4f646252010-04-01 12:45:01 +0000112int set_pages_array_wc(struct page **pages, int addrinarray);
venkatesh.pallipadi@intel.com0f350752009-03-19 14:51:15 -0700113int set_pages_array_wb(struct page **pages, int addrinarray);
114
Arjan van de Ven7219beb2008-04-17 17:41:31 +0200115/*
116 * For legacy compatibility with the old APIs, a few functions
117 * are provided that work on a "struct page".
118 * These functions operate ONLY on the 1:1 kernel mapping of the
119 * memory that the struct page represents, and internally just
120 * call the set_memory_* function. See the description of the
121 * set_memory_* function for more details on conventions.
122 *
123 * These APIs should be considered *deprecated* and are likely going to
124 * be removed in the future.
125 * The reason for this is the implicit operation on the 1:1 mapping only,
126 * making this not a generally useful API.
127 *
128 * Specifically, many users of the old APIs had a virtual address,
129 * called virt_to_page() or vmalloc_to_page() on that address to
130 * get a struct page* that the old API required.
131 * To convert these cases, use set_memory_*() on the original
132 * virtual address, do not use these functions.
133 */
134
135int set_pages_uc(struct page *page, int numpages);
136int set_pages_wb(struct page *page, int numpages);
137int set_pages_x(struct page *page, int numpages);
138int set_pages_nx(struct page *page, int numpages);
139int set_pages_ro(struct page *page, int numpages);
140int set_pages_rw(struct page *page, int numpages);
141
142
Ingo Molnar4c61afc2008-01-30 13:34:09 +0100143void clflush_cache_range(void *addr, unsigned int size);
Thomas Gleixnerb2bba722007-10-15 23:28:20 +0200144
Thomas Gleixnerb2bba722007-10-15 23:28:20 +0200145#ifdef CONFIG_DEBUG_RODATA
146void mark_rodata_ro(void);
Harvey Harrison7bfeab92008-02-12 12:12:01 -0800147extern const int rodata_test_data;
Suresh Siddha502f6602009-10-28 18:46:56 -0800148extern int kernel_set_to_readonly;
Steven Rostedt16239632009-02-17 17:57:30 -0500149void set_kernel_text_rw(void);
150void set_kernel_text_ro(void);
151#else
152static inline void set_kernel_text_rw(void) { }
153static inline void set_kernel_text_ro(void) { }
Thomas Gleixnerb2bba722007-10-15 23:28:20 +0200154#endif
Harvey Harrison7bfeab92008-02-12 12:12:01 -0800155
Arjan van de Venedeed302008-01-30 13:34:08 +0100156#ifdef CONFIG_DEBUG_RODATA_TEST
Harvey Harrison7bfeab92008-02-12 12:12:01 -0800157int rodata_test(void);
Arjan van de Venedeed302008-01-30 13:34:08 +0100158#else
Harvey Harrison7bfeab92008-02-12 12:12:01 -0800159static inline int rodata_test(void)
Arjan van de Venedeed302008-01-30 13:34:08 +0100160{
Harvey Harrison7bfeab92008-02-12 12:12:01 -0800161 return 0;
Arjan van de Venedeed302008-01-30 13:34:08 +0100162}
163#endif
Thomas Gleixnerb2bba722007-10-15 23:28:20 +0200164
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700165#endif /* _ASM_X86_CACHEFLUSH_H */