xref: /openbmc/u-boot/arch/x86/include/asm/mtrr.h (revision 8f3ff11c)
1 /*
2  * Generic MTRR (Memory Type Range Register) ioctls.
3  * Taken from the Linux kernel
4  *
5  * (C) Copyright 2012
6  * Graeme Russ, <graeme.russ@gmail.com>
7  *
8  * Copyright (C) 1997-1999  Richard Gooch <rgooch@atnf.csiro.au>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25 
26 #ifndef _ASM_X86_MTRR_H
27 #define _ASM_X86_MTRR_H
28 
29 #define MTRRphysBase_MSR(reg) (0x200 + 2 * (reg))
30 #define MTRRphysMask_MSR(reg) (0x200 + 2 * (reg) + 1)
31 
32 #ifndef __ASSEMBLY__
33 
34 #include <linux/types.h>
35 #include <linux/ioctl.h>
36 #include <errno.h>
37 
38 #define	MTRR_IOCTL_BASE	'M'
39 
40 struct mtrr_sentry {
41 	unsigned long base;	/*  Base address     */
42 	unsigned int size;	/*  Size of region   */
43 	unsigned int type;	/*  Type of region   */
44 };
45 
46 /*
47  * Warning: this structure has a different order from i386
48  * on x86-64. The 32bit emulation code takes care of that.
49  * But you need to use this for 64bit, otherwise your X server
50  * will break.
51  */
52 
53 #ifdef __i386__
54 struct mtrr_gentry {
55 	unsigned int regnum;	/*  Register number  */
56 	unsigned long base;	/*  Base address     */
57 	unsigned int size;	/*  Size of region   */
58 	unsigned int type;	/*  Type of region   */
59 };
60 
61 #else /* __i386__ */
62 
63 struct mtrr_gentry {
64 	unsigned long base;	/*  Base address     */
65 	unsigned int size;	/*  Size of region   */
66 	unsigned int regnum;	/*  Register number  */
67 	unsigned int type;	/*  Type of region   */
68 };
69 #endif /* !__i386__ */
70 
71 struct mtrr_var_range {
72 	__u32 base_lo;
73 	__u32 base_hi;
74 	__u32 mask_lo;
75 	__u32 mask_hi;
76 };
77 
78 /*
79  * In the Intel processor's MTRR interface, the MTRR type is always held in
80  * an 8 bit field:
81  */
82 typedef __u8 mtrr_type;
83 
84 #define MTRR_NUM_FIXED_RANGES 88
85 #define MTRR_MAX_VAR_RANGES 256
86 
87 struct mtrr_state_type {
88 	struct mtrr_var_range var_ranges[MTRR_MAX_VAR_RANGES];
89 	mtrr_type fixed_ranges[MTRR_NUM_FIXED_RANGES];
90 	unsigned char enabled;
91 	unsigned char have_fixed;
92 	mtrr_type def_type;
93 };
94 
95 /*  These are the various ioctls  */
96 #define MTRRIOC_ADD_ENTRY        _IOW(MTRR_IOCTL_BASE,  0, struct mtrr_sentry)
97 #define MTRRIOC_SET_ENTRY        _IOW(MTRR_IOCTL_BASE,  1, struct mtrr_sentry)
98 #define MTRRIOC_DEL_ENTRY        _IOW(MTRR_IOCTL_BASE,  2, struct mtrr_sentry)
99 #define MTRRIOC_GET_ENTRY        _IOWR(MTRR_IOCTL_BASE, 3, struct mtrr_gentry)
100 #define MTRRIOC_KILL_ENTRY       _IOW(MTRR_IOCTL_BASE,  4, struct mtrr_sentry)
101 #define MTRRIOC_ADD_PAGE_ENTRY   _IOW(MTRR_IOCTL_BASE,  5, struct mtrr_sentry)
102 #define MTRRIOC_SET_PAGE_ENTRY   _IOW(MTRR_IOCTL_BASE,  6, struct mtrr_sentry)
103 #define MTRRIOC_DEL_PAGE_ENTRY   _IOW(MTRR_IOCTL_BASE,  7, struct mtrr_sentry)
104 #define MTRRIOC_GET_PAGE_ENTRY   _IOWR(MTRR_IOCTL_BASE, 8, struct mtrr_gentry)
105 #define MTRRIOC_KILL_PAGE_ENTRY  _IOW(MTRR_IOCTL_BASE,  9, struct mtrr_sentry)
106 
107 /*  These are the region types  */
108 #define MTRR_TYPE_UNCACHABLE 0
109 #define MTRR_TYPE_WRCOMB     1
110 /*#define MTRR_TYPE_         2*/
111 /*#define MTRR_TYPE_         3*/
112 #define MTRR_TYPE_WRTHROUGH  4
113 #define MTRR_TYPE_WRPROT     5
114 #define MTRR_TYPE_WRBACK     6
115 #define MTRR_NUM_TYPES       7
116 
117 #ifdef __KERNEL__
118 
119 /*  The following functions are for use by other drivers  */
120 # ifdef CONFIG_MTRR
121 extern u8 mtrr_type_lookup(u64 addr, u64 end);
122 extern void mtrr_save_fixed_ranges(void *);
123 extern void mtrr_save_state(void);
124 extern int mtrr_add(unsigned long base, unsigned long size,
125 		    unsigned int type, bool increment);
126 extern int mtrr_add_page(unsigned long base, unsigned long size,
127 			 unsigned int type, bool increment);
128 extern int mtrr_del(int reg, unsigned long base, unsigned long size);
129 extern int mtrr_del_page(int reg, unsigned long base, unsigned long size);
130 extern void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi);
131 extern void mtrr_ap_init(void);
132 extern void mtrr_bp_init(void);
133 extern void set_mtrr_aps_delayed_init(void);
134 extern void mtrr_aps_init(void);
135 extern void mtrr_bp_restore(void);
136 extern int mtrr_trim_uncached_memory(unsigned long end_pfn);
137 extern int amd_special_default_mtrr(void);
138 #  else
139 static inline u8 mtrr_type_lookup(u64 addr, u64 end)
140 {
141 	/*
142 	 * Return no-MTRRs:
143 	 */
144 	return 0xff;
145 }
146 #define mtrr_save_fixed_ranges(arg) do {} while (0)
147 #define mtrr_save_state() do {} while (0)
148 static inline int mtrr_del(int reg, unsigned long base, unsigned long size)
149 {
150 	return -ENODEV;
151 }
152 static inline int mtrr_del_page(int reg, unsigned long base, unsigned long size)
153 {
154 	return -ENODEV;
155 }
156 static inline int mtrr_trim_uncached_memory(unsigned long end_pfn)
157 {
158 	return 0;
159 }
160 static inline void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi)
161 {
162 }
163 
164 #define mtrr_ap_init() do {} while (0)
165 #define mtrr_bp_init() do {} while (0)
166 #define set_mtrr_aps_delayed_init() do {} while (0)
167 #define mtrr_aps_init() do {} while (0)
168 #define mtrr_bp_restore() do {} while (0)
169 #  endif
170 
171 #ifdef CONFIG_COMPAT
172 #include <linux/compat.h>
173 
174 struct mtrr_sentry32 {
175 	compat_ulong_t base;	/*  Base address     */
176 	compat_uint_t size;	/*  Size of region   */
177 	compat_uint_t type;	/*  Type of region   */
178 };
179 
180 struct mtrr_gentry32 {
181 	compat_ulong_t regnum;	/*  Register number  */
182 	compat_uint_t base;	/*  Base address     */
183 	compat_uint_t size;	/*  Size of region   */
184 	compat_uint_t type;	/*  Type of region   */
185 };
186 
187 #define MTRR_IOCTL_BASE 'M'
188 
189 #define MTRRIOC32_ADD_ENTRY      _IOW(MTRR_IOCTL_BASE,  0, struct mtrr_sentry32)
190 #define MTRRIOC32_SET_ENTRY      _IOW(MTRR_IOCTL_BASE,  1, struct mtrr_sentry32)
191 #define MTRRIOC32_DEL_ENTRY      _IOW(MTRR_IOCTL_BASE,  2, struct mtrr_sentry32)
192 #define MTRRIOC32_GET_ENTRY      _IOWR(MTRR_IOCTL_BASE, 3, struct mtrr_gentry32)
193 #define MTRRIOC32_KILL_ENTRY     _IOW(MTRR_IOCTL_BASE,  4, struct mtrr_sentry32)
194 #define MTRRIOC32_ADD_PAGE_ENTRY _IOW(MTRR_IOCTL_BASE,  5, struct mtrr_sentry32)
195 #define MTRRIOC32_SET_PAGE_ENTRY _IOW(MTRR_IOCTL_BASE,  6, struct mtrr_sentry32)
196 #define MTRRIOC32_DEL_PAGE_ENTRY _IOW(MTRR_IOCTL_BASE,  7, struct mtrr_sentry32)
197 #define MTRRIOC32_GET_PAGE_ENTRY _IOWR(MTRR_IOCTL_BASE, 8, struct mtrr_gentry32)
198 #define MTRRIOC32_KILL_PAGE_ENTRY		\
199 				 _IOW(MTRR_IOCTL_BASE,  9, struct mtrr_sentry32)
200 #endif /* CONFIG_COMPAT */
201 
202 #endif /* __KERNEL__ */
203 
204 #endif /* __ASSEMBLY__ */
205 
206 #endif /* _ASM_X86_MTRR_H */
207