blob: 4d36dcc1cf87c5b75bcf085df5e77d745a852070 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Randy Dunlapa9415642006-01-11 12:17:48 -08002#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/seq_file.h>
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +05304#include <linux/uaccess.h>
5#include <linux/proc_fs.h>
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +05306#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -08007#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +05309#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#define LINE_SIZE 80
12
13#include <asm/mtrr.h>
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +053014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "mtrr.h"
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
18
Jan Beulich365bff82006-12-07 02:14:09 +010019static const char *const mtrr_strings[MTRR_NUM_TYPES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +053021 "uncachable", /* 0 */
22 "write-combining", /* 1 */
23 "?", /* 2 */
24 "?", /* 3 */
25 "write-through", /* 4 */
26 "write-protect", /* 5 */
27 "write-back", /* 6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028};
29
Jan Beulich365bff82006-12-07 02:14:09 +010030const char *mtrr_attrib_to_str(int x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 return (x <= 6) ? mtrr_strings[x] : "?";
33}
34
35#ifdef CONFIG_PROC_FS
36
37static int
38mtrr_file_add(unsigned long base, unsigned long size,
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +010039 unsigned int type, bool increment, struct file *file, int page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +053041 unsigned int *fcount = FILE_FCOUNT(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 int reg, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 max = num_var_ranges;
45 if (fcount == NULL) {
Kees Cook6396bb22018-06-12 14:03:40 -070046 fcount = kcalloc(max, sizeof(*fcount), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (!fcount)
48 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 FILE_FCOUNT(file) = fcount;
50 }
51 if (!page) {
52 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
53 return -EINVAL;
54 base >>= PAGE_SHIFT;
55 size >>= PAGE_SHIFT;
56 }
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +010057 reg = mtrr_add_page(base, size, type, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (reg >= 0)
59 ++fcount[reg];
60 return reg;
61}
62
63static int
64mtrr_file_del(unsigned long base, unsigned long size,
65 struct file *file, int page)
66{
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned int *fcount = FILE_FCOUNT(file);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +053068 int reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 if (!page) {
71 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
72 return -EINVAL;
73 base >>= PAGE_SHIFT;
74 size >>= PAGE_SHIFT;
75 }
76 reg = mtrr_del_page(-1, base, size);
77 if (reg < 0)
78 return reg;
79 if (fcount == NULL)
80 return reg;
81 if (fcount[reg] < 1)
82 return -EINVAL;
83 --fcount[reg];
84 return reg;
85}
86
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +053087/*
88 * seq_file can seek but we ignore it.
89 *
90 * Format of control line:
91 * "base=%Lx size=%Lx type=%s" or "disable=%d"
92 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static ssize_t
94mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int i, err;
97 unsigned long reg;
98 unsigned long long base, size;
99 char *ptr;
100 char line[LINE_SIZE];
Arjan van de Ven11879ba2009-09-26 20:51:50 +0200101 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 size_t linelen;
103
104 if (!capable(CAP_SYS_ADMIN))
105 return -EPERM;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 memset(line, 0, LINE_SIZE);
Arjan van de Ven11879ba2009-09-26 20:51:50 +0200108
Jann Horn15279df2018-07-06 23:50:03 +0200109 len = min_t(size_t, len, LINE_SIZE - 1);
110 length = strncpy_from_user(line, buf, len);
Arjan van de Ven11879ba2009-09-26 20:51:50 +0200111 if (length < 0)
Andy Shevchenko7f8ec5a2018-05-15 21:05:35 +0300112 return length;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 linelen = strlen(line);
115 ptr = line + linelen - 1;
116 if (linelen && *ptr == '\n')
117 *ptr = '\0';
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!strncmp(line, "disable=", 8)) {
120 reg = simple_strtoul(line + 8, &ptr, 0);
121 err = mtrr_del_page(reg, 0, 0);
122 if (err < 0)
123 return err;
124 return len;
125 }
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (strncmp(line, "base=", 5))
128 return -EINVAL;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 base = simple_strtoull(line + 5, &ptr, 0);
André Goddard Rosae7d28602009-12-14 18:01:06 -0800131 ptr = skip_spaces(ptr);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (strncmp(ptr, "size=", 5))
134 return -EINVAL;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 size = simple_strtoull(ptr + 5, &ptr, 0);
137 if ((base & 0xfff) || (size & 0xfff))
138 return -EINVAL;
André Goddard Rosae7d28602009-12-14 18:01:06 -0800139 ptr = skip_spaces(ptr);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (strncmp(ptr, "type=", 5))
142 return -EINVAL;
André Goddard Rosae7d28602009-12-14 18:01:06 -0800143 ptr = skip_spaces(ptr + 5);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530144
Andy Shevchenko13a4db92018-05-15 20:57:59 +0300145 i = match_string(mtrr_strings, MTRR_NUM_TYPES, ptr);
146 if (i < 0)
147 return i;
148
149 base >>= PAGE_SHIFT;
150 size >>= PAGE_SHIFT;
151 err = mtrr_add_page((unsigned long)base, (unsigned long)size, i, true);
152 if (err < 0)
153 return err;
154 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Brian Gerstc5311782005-10-30 14:59:44 -0800157static long
158mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Brian Gerstc5311782005-10-30 14:59:44 -0800160 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 mtrr_type type;
H. Peter Anvinb263b312012-02-27 15:15:25 -0800162 unsigned long base;
Jan Beulich365bff82006-12-07 02:14:09 +0100163 unsigned long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct mtrr_sentry sentry;
165 struct mtrr_gentry gentry;
166 void __user *arg = (void __user *) __arg;
167
Colin Ian King32043fa2018-12-18 17:29:56 +0000168 memset(&gentry, 0, sizeof(gentry));
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 switch (cmd) {
Brian Gerstc5311782005-10-30 14:59:44 -0800171 case MTRRIOC_ADD_ENTRY:
172 case MTRRIOC_SET_ENTRY:
173 case MTRRIOC_DEL_ENTRY:
174 case MTRRIOC_KILL_ENTRY:
175 case MTRRIOC_ADD_PAGE_ENTRY:
176 case MTRRIOC_SET_PAGE_ENTRY:
177 case MTRRIOC_DEL_PAGE_ENTRY:
178 case MTRRIOC_KILL_PAGE_ENTRY:
Jordan Borgner0e96f312018-10-28 12:58:28 +0000179 if (copy_from_user(&sentry, arg, sizeof(sentry)))
Brian Gerstc5311782005-10-30 14:59:44 -0800180 return -EFAULT;
181 break;
182 case MTRRIOC_GET_ENTRY:
183 case MTRRIOC_GET_PAGE_ENTRY:
Jordan Borgner0e96f312018-10-28 12:58:28 +0000184 if (copy_from_user(&gentry, arg, sizeof(gentry)))
Brian Gerstc5311782005-10-30 14:59:44 -0800185 return -EFAULT;
186 break;
187#ifdef CONFIG_COMPAT
188 case MTRRIOC32_ADD_ENTRY:
189 case MTRRIOC32_SET_ENTRY:
190 case MTRRIOC32_DEL_ENTRY:
191 case MTRRIOC32_KILL_ENTRY:
192 case MTRRIOC32_ADD_PAGE_ENTRY:
193 case MTRRIOC32_SET_PAGE_ENTRY:
194 case MTRRIOC32_DEL_PAGE_ENTRY:
195 case MTRRIOC32_KILL_PAGE_ENTRY: {
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530196 struct mtrr_sentry32 __user *s32;
197
198 s32 = (struct mtrr_sentry32 __user *)__arg;
Brian Gerstc5311782005-10-30 14:59:44 -0800199 err = get_user(sentry.base, &s32->base);
200 err |= get_user(sentry.size, &s32->size);
201 err |= get_user(sentry.type, &s32->type);
202 if (err)
203 return err;
204 break;
205 }
206 case MTRRIOC32_GET_ENTRY:
207 case MTRRIOC32_GET_PAGE_ENTRY: {
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530208 struct mtrr_gentry32 __user *g32;
209
210 g32 = (struct mtrr_gentry32 __user *)__arg;
Brian Gerstc5311782005-10-30 14:59:44 -0800211 err = get_user(gentry.regnum, &g32->regnum);
212 err |= get_user(gentry.base, &g32->base);
213 err |= get_user(gentry.size, &g32->size);
214 err |= get_user(gentry.type, &g32->type);
215 if (err)
216 return err;
217 break;
218 }
219#endif
220 }
221
222 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 default:
224 return -ENOTTY;
225 case MTRRIOC_ADD_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100226#ifdef CONFIG_COMPAT
227 case MTRRIOC32_ADD_ENTRY:
228#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!capable(CAP_SYS_ADMIN))
230 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 err =
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100232 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 file, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
235 case MTRRIOC_SET_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100236#ifdef CONFIG_COMPAT
237 case MTRRIOC32_SET_ENTRY:
238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!capable(CAP_SYS_ADMIN))
240 return -EPERM;
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100241 err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 break;
243 case MTRRIOC_DEL_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100244#ifdef CONFIG_COMPAT
245 case MTRRIOC32_DEL_ENTRY:
246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!capable(CAP_SYS_ADMIN))
248 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251 case MTRRIOC_KILL_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100252#ifdef CONFIG_COMPAT
253 case MTRRIOC32_KILL_ENTRY:
254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!capable(CAP_SYS_ADMIN))
256 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 err = mtrr_del(-1, sentry.base, sentry.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
259 case MTRRIOC_GET_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100260#ifdef CONFIG_COMPAT
261 case MTRRIOC32_GET_ENTRY:
262#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (gentry.regnum >= num_var_ranges)
264 return -EINVAL;
H. Peter Anvinb263b312012-02-27 15:15:25 -0800265 mtrr_if->get(gentry.regnum, &base, &size, &type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* Hide entries that go above 4GB */
H. Peter Anvinb263b312012-02-27 15:15:25 -0800268 if (base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
Jan Beulich365bff82006-12-07 02:14:09 +0100269 || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 gentry.base = gentry.size = gentry.type = 0;
271 else {
H. Peter Anvinb263b312012-02-27 15:15:25 -0800272 gentry.base = base << PAGE_SHIFT;
Jan Beulich365bff82006-12-07 02:14:09 +0100273 gentry.size = size << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 gentry.type = type;
275 }
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 case MTRRIOC_ADD_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100279#ifdef CONFIG_COMPAT
280 case MTRRIOC32_ADD_PAGE_ENTRY:
281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!capable(CAP_SYS_ADMIN))
283 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 err =
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100285 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 file, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
288 case MTRRIOC_SET_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100289#ifdef CONFIG_COMPAT
290 case MTRRIOC32_SET_PAGE_ENTRY:
291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!capable(CAP_SYS_ADMIN))
293 return -EPERM;
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100294 err =
295 mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 case MTRRIOC_DEL_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100298#ifdef CONFIG_COMPAT
299 case MTRRIOC32_DEL_PAGE_ENTRY:
300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!capable(CAP_SYS_ADMIN))
302 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 case MTRRIOC_KILL_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100306#ifdef CONFIG_COMPAT
307 case MTRRIOC32_KILL_PAGE_ENTRY:
308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!capable(CAP_SYS_ADMIN))
310 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 err = mtrr_del_page(-1, sentry.base, sentry.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
313 case MTRRIOC_GET_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100314#ifdef CONFIG_COMPAT
315 case MTRRIOC32_GET_PAGE_ENTRY:
316#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (gentry.regnum >= num_var_ranges)
318 return -EINVAL;
H. Peter Anvinb263b312012-02-27 15:15:25 -0800319 mtrr_if->get(gentry.regnum, &base, &size, &type);
Jan Beulich365bff82006-12-07 02:14:09 +0100320 /* Hide entries that would overflow */
321 if (size != (__typeof__(gentry.size))size)
322 gentry.base = gentry.size = gentry.type = 0;
323 else {
H. Peter Anvinb263b312012-02-27 15:15:25 -0800324 gentry.base = base;
Jan Beulich365bff82006-12-07 02:14:09 +0100325 gentry.size = size;
326 gentry.type = type;
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
329 }
Brian Gerstc5311782005-10-30 14:59:44 -0800330
331 if (err)
332 return err;
333
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530334 switch (cmd) {
Brian Gerstc5311782005-10-30 14:59:44 -0800335 case MTRRIOC_GET_ENTRY:
336 case MTRRIOC_GET_PAGE_ENTRY:
Jordan Borgner0e96f312018-10-28 12:58:28 +0000337 if (copy_to_user(arg, &gentry, sizeof(gentry)))
Brian Gerstc5311782005-10-30 14:59:44 -0800338 err = -EFAULT;
339 break;
340#ifdef CONFIG_COMPAT
341 case MTRRIOC32_GET_ENTRY:
342 case MTRRIOC32_GET_PAGE_ENTRY: {
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530343 struct mtrr_gentry32 __user *g32;
344
345 g32 = (struct mtrr_gentry32 __user *)__arg;
Brian Gerstc5311782005-10-30 14:59:44 -0800346 err = put_user(gentry.base, &g32->base);
347 err |= put_user(gentry.size, &g32->size);
348 err |= put_user(gentry.regnum, &g32->regnum);
349 err |= put_user(gentry.type, &g32->type);
350 break;
351 }
352#endif
353 }
354 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530357static int mtrr_close(struct inode *ino, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned int *fcount = FILE_FCOUNT(file);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530360 int i, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if (fcount != NULL) {
363 max = num_var_ranges;
364 for (i = 0; i < max; ++i) {
365 while (fcount[i] > 0) {
366 mtrr_del(i, 0, 0);
367 --fcount[i];
368 }
369 }
370 kfree(fcount);
371 FILE_FCOUNT(file) = NULL;
372 }
373 return single_release(ino, file);
374}
375
376static int mtrr_seq_show(struct seq_file *seq, void *offset);
377
378static int mtrr_open(struct inode *inode, struct file *file)
379{
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530380 if (!mtrr_if)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return -EIO;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530382 if (!mtrr_if->get)
383 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return single_open(file, mtrr_seq_show, NULL);
385}
386
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800387static const struct file_operations mtrr_fops = {
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530388 .owner = THIS_MODULE,
389 .open = mtrr_open,
390 .read = seq_read,
391 .llseek = seq_lseek,
392 .write = mtrr_write,
393 .unlocked_ioctl = mtrr_ioctl,
394 .compat_ioctl = mtrr_ioctl,
395 .release = mtrr_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static int mtrr_seq_show(struct seq_file *seq, void *offset)
399{
400 char factor;
Joe Perches3ac62bc2015-04-15 16:17:45 -0700401 int i, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 mtrr_type type;
Jan Beulich365bff82006-12-07 02:14:09 +0100403 unsigned long base, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 max = num_var_ranges;
406 for (i = 0; i < max; i++) {
407 mtrr_if->get(i, &base, &size, &type);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530408 if (size == 0) {
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100409 mtrr_usage_table[i] = 0;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530410 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530412 if (size < (0x100000 >> PAGE_SHIFT)) {
413 /* less than 1MB */
414 factor = 'K';
415 size <<= PAGE_SHIFT - 10;
416 } else {
417 factor = 'M';
418 size >>= 20 - PAGE_SHIFT;
419 }
420 /* Base can be > 32bit */
Joe Perches3ac62bc2015-04-15 16:17:45 -0700421 seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
422 i, base, base >> (20 - PAGE_SHIFT),
423 size, factor,
424 mtrr_usage_table[i], mtrr_attrib_to_str(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 return 0;
427}
428
429static int __init mtrr_if_init(void)
430{
431 struct cpuinfo_x86 *c = &boot_cpu_data;
432
433 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
434 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
435 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
436 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
437 return -ENODEV;
438
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300439 proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return 0;
441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442arch_initcall(mtrr_if_init);
443#endif /* CONFIG_PROC_FS */