blob: 42b4f2f3b55733189f25efae167b2784aa697478 [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) {
Burman Yan9cfa5b52006-12-07 02:14:13 +010046 fcount = kzalloc(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
109 length = len;
110 length--;
111
112 if (length > LINE_SIZE - 1)
113 length = LINE_SIZE - 1;
114
115 if (length < 0)
116 return -EINVAL;
117
118 if (copy_from_user(line, buf, length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return -EFAULT;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 linelen = strlen(line);
122 ptr = line + linelen - 1;
123 if (linelen && *ptr == '\n')
124 *ptr = '\0';
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (!strncmp(line, "disable=", 8)) {
127 reg = simple_strtoul(line + 8, &ptr, 0);
128 err = mtrr_del_page(reg, 0, 0);
129 if (err < 0)
130 return err;
131 return len;
132 }
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (strncmp(line, "base=", 5))
135 return -EINVAL;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 base = simple_strtoull(line + 5, &ptr, 0);
André Goddard Rosae7d28602009-12-14 18:01:06 -0800138 ptr = skip_spaces(ptr);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (strncmp(ptr, "size=", 5))
141 return -EINVAL;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 size = simple_strtoull(ptr + 5, &ptr, 0);
144 if ((base & 0xfff) || (size & 0xfff))
145 return -EINVAL;
André Goddard Rosae7d28602009-12-14 18:01:06 -0800146 ptr = skip_spaces(ptr);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (strncmp(ptr, "type=", 5))
149 return -EINVAL;
André Goddard Rosae7d28602009-12-14 18:01:06 -0800150 ptr = skip_spaces(ptr + 5);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530151
Andy Shevchenko13a4db92018-05-15 20:57:59 +0300152 i = match_string(mtrr_strings, MTRR_NUM_TYPES, ptr);
153 if (i < 0)
154 return i;
155
156 base >>= PAGE_SHIFT;
157 size >>= PAGE_SHIFT;
158 err = mtrr_add_page((unsigned long)base, (unsigned long)size, i, true);
159 if (err < 0)
160 return err;
161 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Brian Gerstc5311782005-10-30 14:59:44 -0800164static long
165mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Brian Gerstc5311782005-10-30 14:59:44 -0800167 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 mtrr_type type;
H. Peter Anvinb263b312012-02-27 15:15:25 -0800169 unsigned long base;
Jan Beulich365bff82006-12-07 02:14:09 +0100170 unsigned long size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct mtrr_sentry sentry;
172 struct mtrr_gentry gentry;
173 void __user *arg = (void __user *) __arg;
174
175 switch (cmd) {
Brian Gerstc5311782005-10-30 14:59:44 -0800176 case MTRRIOC_ADD_ENTRY:
177 case MTRRIOC_SET_ENTRY:
178 case MTRRIOC_DEL_ENTRY:
179 case MTRRIOC_KILL_ENTRY:
180 case MTRRIOC_ADD_PAGE_ENTRY:
181 case MTRRIOC_SET_PAGE_ENTRY:
182 case MTRRIOC_DEL_PAGE_ENTRY:
183 case MTRRIOC_KILL_PAGE_ENTRY:
184 if (copy_from_user(&sentry, arg, sizeof sentry))
185 return -EFAULT;
186 break;
187 case MTRRIOC_GET_ENTRY:
188 case MTRRIOC_GET_PAGE_ENTRY:
189 if (copy_from_user(&gentry, arg, sizeof gentry))
190 return -EFAULT;
191 break;
192#ifdef CONFIG_COMPAT
193 case MTRRIOC32_ADD_ENTRY:
194 case MTRRIOC32_SET_ENTRY:
195 case MTRRIOC32_DEL_ENTRY:
196 case MTRRIOC32_KILL_ENTRY:
197 case MTRRIOC32_ADD_PAGE_ENTRY:
198 case MTRRIOC32_SET_PAGE_ENTRY:
199 case MTRRIOC32_DEL_PAGE_ENTRY:
200 case MTRRIOC32_KILL_PAGE_ENTRY: {
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530201 struct mtrr_sentry32 __user *s32;
202
203 s32 = (struct mtrr_sentry32 __user *)__arg;
Brian Gerstc5311782005-10-30 14:59:44 -0800204 err = get_user(sentry.base, &s32->base);
205 err |= get_user(sentry.size, &s32->size);
206 err |= get_user(sentry.type, &s32->type);
207 if (err)
208 return err;
209 break;
210 }
211 case MTRRIOC32_GET_ENTRY:
212 case MTRRIOC32_GET_PAGE_ENTRY: {
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530213 struct mtrr_gentry32 __user *g32;
214
215 g32 = (struct mtrr_gentry32 __user *)__arg;
Brian Gerstc5311782005-10-30 14:59:44 -0800216 err = get_user(gentry.regnum, &g32->regnum);
217 err |= get_user(gentry.base, &g32->base);
218 err |= get_user(gentry.size, &g32->size);
219 err |= get_user(gentry.type, &g32->type);
220 if (err)
221 return err;
222 break;
223 }
224#endif
225 }
226
227 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 default:
229 return -ENOTTY;
230 case MTRRIOC_ADD_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100231#ifdef CONFIG_COMPAT
232 case MTRRIOC32_ADD_ENTRY:
233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!capable(CAP_SYS_ADMIN))
235 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 err =
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100237 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 file, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 break;
240 case MTRRIOC_SET_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100241#ifdef CONFIG_COMPAT
242 case MTRRIOC32_SET_ENTRY:
243#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!capable(CAP_SYS_ADMIN))
245 return -EPERM;
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100246 err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 break;
248 case MTRRIOC_DEL_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100249#ifdef CONFIG_COMPAT
250 case MTRRIOC32_DEL_ENTRY:
251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!capable(CAP_SYS_ADMIN))
253 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256 case MTRRIOC_KILL_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100257#ifdef CONFIG_COMPAT
258 case MTRRIOC32_KILL_ENTRY:
259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!capable(CAP_SYS_ADMIN))
261 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 err = mtrr_del(-1, sentry.base, sentry.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 break;
264 case MTRRIOC_GET_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100265#ifdef CONFIG_COMPAT
266 case MTRRIOC32_GET_ENTRY:
267#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (gentry.regnum >= num_var_ranges)
269 return -EINVAL;
H. Peter Anvinb263b312012-02-27 15:15:25 -0800270 mtrr_if->get(gentry.regnum, &base, &size, &type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* Hide entries that go above 4GB */
H. Peter Anvinb263b312012-02-27 15:15:25 -0800273 if (base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
Jan Beulich365bff82006-12-07 02:14:09 +0100274 || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 gentry.base = gentry.size = gentry.type = 0;
276 else {
H. Peter Anvinb263b312012-02-27 15:15:25 -0800277 gentry.base = base << PAGE_SHIFT;
Jan Beulich365bff82006-12-07 02:14:09 +0100278 gentry.size = size << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 gentry.type = type;
280 }
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 case MTRRIOC_ADD_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100284#ifdef CONFIG_COMPAT
285 case MTRRIOC32_ADD_PAGE_ENTRY:
286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!capable(CAP_SYS_ADMIN))
288 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 err =
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100290 mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 file, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 break;
293 case MTRRIOC_SET_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100294#ifdef CONFIG_COMPAT
295 case MTRRIOC32_SET_PAGE_ENTRY:
296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (!capable(CAP_SYS_ADMIN))
298 return -EPERM;
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100299 err =
300 mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 case MTRRIOC_DEL_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100303#ifdef CONFIG_COMPAT
304 case MTRRIOC32_DEL_PAGE_ENTRY:
305#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!capable(CAP_SYS_ADMIN))
307 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 break;
310 case MTRRIOC_KILL_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100311#ifdef CONFIG_COMPAT
312 case MTRRIOC32_KILL_PAGE_ENTRY:
313#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!capable(CAP_SYS_ADMIN))
315 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 err = mtrr_del_page(-1, sentry.base, sentry.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318 case MTRRIOC_GET_PAGE_ENTRY:
Giuliano Procida98838ec2007-02-13 13:26:26 +0100319#ifdef CONFIG_COMPAT
320 case MTRRIOC32_GET_PAGE_ENTRY:
321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (gentry.regnum >= num_var_ranges)
323 return -EINVAL;
H. Peter Anvinb263b312012-02-27 15:15:25 -0800324 mtrr_if->get(gentry.regnum, &base, &size, &type);
Jan Beulich365bff82006-12-07 02:14:09 +0100325 /* Hide entries that would overflow */
326 if (size != (__typeof__(gentry.size))size)
327 gentry.base = gentry.size = gentry.type = 0;
328 else {
H. Peter Anvinb263b312012-02-27 15:15:25 -0800329 gentry.base = base;
Jan Beulich365bff82006-12-07 02:14:09 +0100330 gentry.size = size;
331 gentry.type = type;
332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 break;
334 }
Brian Gerstc5311782005-10-30 14:59:44 -0800335
336 if (err)
337 return err;
338
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530339 switch (cmd) {
Brian Gerstc5311782005-10-30 14:59:44 -0800340 case MTRRIOC_GET_ENTRY:
341 case MTRRIOC_GET_PAGE_ENTRY:
342 if (copy_to_user(arg, &gentry, sizeof gentry))
343 err = -EFAULT;
344 break;
345#ifdef CONFIG_COMPAT
346 case MTRRIOC32_GET_ENTRY:
347 case MTRRIOC32_GET_PAGE_ENTRY: {
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530348 struct mtrr_gentry32 __user *g32;
349
350 g32 = (struct mtrr_gentry32 __user *)__arg;
Brian Gerstc5311782005-10-30 14:59:44 -0800351 err = put_user(gentry.base, &g32->base);
352 err |= put_user(gentry.size, &g32->size);
353 err |= put_user(gentry.regnum, &g32->regnum);
354 err |= put_user(gentry.type, &g32->type);
355 break;
356 }
357#endif
358 }
359 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530362static int mtrr_close(struct inode *ino, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 unsigned int *fcount = FILE_FCOUNT(file);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530365 int i, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (fcount != NULL) {
368 max = num_var_ranges;
369 for (i = 0; i < max; ++i) {
370 while (fcount[i] > 0) {
371 mtrr_del(i, 0, 0);
372 --fcount[i];
373 }
374 }
375 kfree(fcount);
376 FILE_FCOUNT(file) = NULL;
377 }
378 return single_release(ino, file);
379}
380
381static int mtrr_seq_show(struct seq_file *seq, void *offset);
382
383static int mtrr_open(struct inode *inode, struct file *file)
384{
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530385 if (!mtrr_if)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -EIO;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530387 if (!mtrr_if->get)
388 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return single_open(file, mtrr_seq_show, NULL);
390}
391
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800392static const struct file_operations mtrr_fops = {
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530393 .owner = THIS_MODULE,
394 .open = mtrr_open,
395 .read = seq_read,
396 .llseek = seq_lseek,
397 .write = mtrr_write,
398 .unlocked_ioctl = mtrr_ioctl,
399 .compat_ioctl = mtrr_ioctl,
400 .release = mtrr_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static int mtrr_seq_show(struct seq_file *seq, void *offset)
404{
405 char factor;
Joe Perches3ac62bc2015-04-15 16:17:45 -0700406 int i, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 mtrr_type type;
Jan Beulich365bff82006-12-07 02:14:09 +0100408 unsigned long base, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 max = num_var_ranges;
411 for (i = 0; i < max; i++) {
412 mtrr_if->get(i, &base, &size, &type);
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530413 if (size == 0) {
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100414 mtrr_usage_table[i] = 0;
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530415 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Jaswinder Singh Rajput26dc67e2009-07-04 07:53:40 +0530417 if (size < (0x100000 >> PAGE_SHIFT)) {
418 /* less than 1MB */
419 factor = 'K';
420 size <<= PAGE_SHIFT - 10;
421 } else {
422 factor = 'M';
423 size >>= 20 - PAGE_SHIFT;
424 }
425 /* Base can be > 32bit */
Joe Perches3ac62bc2015-04-15 16:17:45 -0700426 seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
427 i, base, base >> (20 - PAGE_SHIFT),
428 size, factor,
429 mtrr_usage_table[i], mtrr_attrib_to_str(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 return 0;
432}
433
434static int __init mtrr_if_init(void)
435{
436 struct cpuinfo_x86 *c = &boot_cpu_data;
437
438 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
439 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
440 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
441 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
442 return -ENODEV;
443
Alexey Dobriyan99b76232009-03-25 22:48:06 +0300444 proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447arch_initcall(mtrr_if_init);
448#endif /* CONFIG_PROC_FS */