[PATCH] i386/x86-64: Fix NMI watchdog suspend/resume
[linux-2.6/next.git] / arch / i386 / kernel / cpu / mtrr / if.c
blob5ac051bb9d55e36b4daf4a1a06e072992728d18e
1 #include <linux/init.h>
2 #include <linux/proc_fs.h>
3 #include <linux/capability.h>
4 #include <linux/ctype.h>
5 #include <linux/module.h>
6 #include <linux/seq_file.h>
7 #include <asm/uaccess.h>
9 #define LINE_SIZE 80
11 #include <asm/mtrr.h>
12 #include "mtrr.h"
14 /* RED-PEN: this is accessed without any locking */
15 extern unsigned int *usage_table;
18 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
20 static char *mtrr_strings[MTRR_NUM_TYPES] =
22 "uncachable", /* 0 */
23 "write-combining", /* 1 */
24 "?", /* 2 */
25 "?", /* 3 */
26 "write-through", /* 4 */
27 "write-protect", /* 5 */
28 "write-back", /* 6 */
31 char *mtrr_attrib_to_str(int x)
33 return (x <= 6) ? mtrr_strings[x] : "?";
36 #ifdef CONFIG_PROC_FS
38 static int
39 mtrr_file_add(unsigned long base, unsigned long size,
40 unsigned int type, char increment, struct file *file, int page)
42 int reg, max;
43 unsigned int *fcount = FILE_FCOUNT(file);
45 max = num_var_ranges;
46 if (fcount == NULL) {
47 fcount = kmalloc(max * sizeof *fcount, GFP_KERNEL);
48 if (!fcount)
49 return -ENOMEM;
50 memset(fcount, 0, max * sizeof *fcount);
51 FILE_FCOUNT(file) = fcount;
53 if (!page) {
54 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
55 return -EINVAL;
56 base >>= PAGE_SHIFT;
57 size >>= PAGE_SHIFT;
59 reg = mtrr_add_page(base, size, type, 1);
60 if (reg >= 0)
61 ++fcount[reg];
62 return reg;
65 static int
66 mtrr_file_del(unsigned long base, unsigned long size,
67 struct file *file, int page)
69 int reg;
70 unsigned int *fcount = FILE_FCOUNT(file);
72 if (!page) {
73 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
74 return -EINVAL;
75 base >>= PAGE_SHIFT;
76 size >>= PAGE_SHIFT;
78 reg = mtrr_del_page(-1, base, size);
79 if (reg < 0)
80 return reg;
81 if (fcount == NULL)
82 return reg;
83 if (fcount[reg] < 1)
84 return -EINVAL;
85 --fcount[reg];
86 return reg;
89 /* RED-PEN: seq_file can seek now. this is ignored. */
90 static ssize_t
91 mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
92 /* Format of control line:
93 "base=%Lx size=%Lx type=%s" OR:
94 "disable=%d"
97 int i, err;
98 unsigned long reg;
99 unsigned long long base, size;
100 char *ptr;
101 char line[LINE_SIZE];
102 size_t linelen;
104 if (!capable(CAP_SYS_ADMIN))
105 return -EPERM;
106 if (!len)
107 return -EINVAL;
108 memset(line, 0, LINE_SIZE);
109 if (len > LINE_SIZE)
110 len = LINE_SIZE;
111 if (copy_from_user(line, buf, len - 1))
112 return -EFAULT;
113 linelen = strlen(line);
114 ptr = line + linelen - 1;
115 if (linelen && *ptr == '\n')
116 *ptr = '\0';
117 if (!strncmp(line, "disable=", 8)) {
118 reg = simple_strtoul(line + 8, &ptr, 0);
119 err = mtrr_del_page(reg, 0, 0);
120 if (err < 0)
121 return err;
122 return len;
124 if (strncmp(line, "base=", 5))
125 return -EINVAL;
126 base = simple_strtoull(line + 5, &ptr, 0);
127 for (; isspace(*ptr); ++ptr) ;
128 if (strncmp(ptr, "size=", 5))
129 return -EINVAL;
130 size = simple_strtoull(ptr + 5, &ptr, 0);
131 if ((base & 0xfff) || (size & 0xfff))
132 return -EINVAL;
133 for (; isspace(*ptr); ++ptr) ;
134 if (strncmp(ptr, "type=", 5))
135 return -EINVAL;
136 ptr += 5;
137 for (; isspace(*ptr); ++ptr) ;
138 for (i = 0; i < MTRR_NUM_TYPES; ++i) {
139 if (strcmp(ptr, mtrr_strings[i]))
140 continue;
141 base >>= PAGE_SHIFT;
142 size >>= PAGE_SHIFT;
143 err =
144 mtrr_add_page((unsigned long) base, (unsigned long) size, i,
146 if (err < 0)
147 return err;
148 return len;
150 return -EINVAL;
153 static long
154 mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
156 int err = 0;
157 mtrr_type type;
158 struct mtrr_sentry sentry;
159 struct mtrr_gentry gentry;
160 void __user *arg = (void __user *) __arg;
162 switch (cmd) {
163 case MTRRIOC_ADD_ENTRY:
164 case MTRRIOC_SET_ENTRY:
165 case MTRRIOC_DEL_ENTRY:
166 case MTRRIOC_KILL_ENTRY:
167 case MTRRIOC_ADD_PAGE_ENTRY:
168 case MTRRIOC_SET_PAGE_ENTRY:
169 case MTRRIOC_DEL_PAGE_ENTRY:
170 case MTRRIOC_KILL_PAGE_ENTRY:
171 if (copy_from_user(&sentry, arg, sizeof sentry))
172 return -EFAULT;
173 break;
174 case MTRRIOC_GET_ENTRY:
175 case MTRRIOC_GET_PAGE_ENTRY:
176 if (copy_from_user(&gentry, arg, sizeof gentry))
177 return -EFAULT;
178 break;
179 #ifdef CONFIG_COMPAT
180 case MTRRIOC32_ADD_ENTRY:
181 case MTRRIOC32_SET_ENTRY:
182 case MTRRIOC32_DEL_ENTRY:
183 case MTRRIOC32_KILL_ENTRY:
184 case MTRRIOC32_ADD_PAGE_ENTRY:
185 case MTRRIOC32_SET_PAGE_ENTRY:
186 case MTRRIOC32_DEL_PAGE_ENTRY:
187 case MTRRIOC32_KILL_PAGE_ENTRY: {
188 struct mtrr_sentry32 __user *s32 = (struct mtrr_sentry32 __user *)__arg;
189 err = get_user(sentry.base, &s32->base);
190 err |= get_user(sentry.size, &s32->size);
191 err |= get_user(sentry.type, &s32->type);
192 if (err)
193 return err;
194 break;
196 case MTRRIOC32_GET_ENTRY:
197 case MTRRIOC32_GET_PAGE_ENTRY: {
198 struct mtrr_gentry32 __user *g32 = (struct mtrr_gentry32 __user *)__arg;
199 err = get_user(gentry.regnum, &g32->regnum);
200 err |= get_user(gentry.base, &g32->base);
201 err |= get_user(gentry.size, &g32->size);
202 err |= get_user(gentry.type, &g32->type);
203 if (err)
204 return err;
205 break;
207 #endif
210 switch (cmd) {
211 default:
212 return -ENOTTY;
213 case MTRRIOC_ADD_ENTRY:
214 if (!capable(CAP_SYS_ADMIN))
215 return -EPERM;
216 err =
217 mtrr_file_add(sentry.base, sentry.size, sentry.type, 1,
218 file, 0);
219 break;
220 case MTRRIOC_SET_ENTRY:
221 if (!capable(CAP_SYS_ADMIN))
222 return -EPERM;
223 err = mtrr_add(sentry.base, sentry.size, sentry.type, 0);
224 break;
225 case MTRRIOC_DEL_ENTRY:
226 if (!capable(CAP_SYS_ADMIN))
227 return -EPERM;
228 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
229 break;
230 case MTRRIOC_KILL_ENTRY:
231 if (!capable(CAP_SYS_ADMIN))
232 return -EPERM;
233 err = mtrr_del(-1, sentry.base, sentry.size);
234 break;
235 case MTRRIOC_GET_ENTRY:
236 if (gentry.regnum >= num_var_ranges)
237 return -EINVAL;
238 mtrr_if->get(gentry.regnum, &gentry.base, &gentry.size, &type);
240 /* Hide entries that go above 4GB */
241 if (gentry.base + gentry.size > 0x100000
242 || gentry.size == 0x100000)
243 gentry.base = gentry.size = gentry.type = 0;
244 else {
245 gentry.base <<= PAGE_SHIFT;
246 gentry.size <<= PAGE_SHIFT;
247 gentry.type = type;
250 break;
251 case MTRRIOC_ADD_PAGE_ENTRY:
252 if (!capable(CAP_SYS_ADMIN))
253 return -EPERM;
254 err =
255 mtrr_file_add(sentry.base, sentry.size, sentry.type, 1,
256 file, 1);
257 break;
258 case MTRRIOC_SET_PAGE_ENTRY:
259 if (!capable(CAP_SYS_ADMIN))
260 return -EPERM;
261 err = mtrr_add_page(sentry.base, sentry.size, sentry.type, 0);
262 break;
263 case MTRRIOC_DEL_PAGE_ENTRY:
264 if (!capable(CAP_SYS_ADMIN))
265 return -EPERM;
266 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
267 break;
268 case MTRRIOC_KILL_PAGE_ENTRY:
269 if (!capable(CAP_SYS_ADMIN))
270 return -EPERM;
271 err = mtrr_del_page(-1, sentry.base, sentry.size);
272 break;
273 case MTRRIOC_GET_PAGE_ENTRY:
274 if (gentry.regnum >= num_var_ranges)
275 return -EINVAL;
276 mtrr_if->get(gentry.regnum, &gentry.base, &gentry.size, &type);
277 gentry.type = type;
278 break;
281 if (err)
282 return err;
284 switch(cmd) {
285 case MTRRIOC_GET_ENTRY:
286 case MTRRIOC_GET_PAGE_ENTRY:
287 if (copy_to_user(arg, &gentry, sizeof gentry))
288 err = -EFAULT;
289 break;
290 #ifdef CONFIG_COMPAT
291 case MTRRIOC32_GET_ENTRY:
292 case MTRRIOC32_GET_PAGE_ENTRY: {
293 struct mtrr_gentry32 __user *g32 = (struct mtrr_gentry32 __user *)__arg;
294 err = put_user(gentry.base, &g32->base);
295 err |= put_user(gentry.size, &g32->size);
296 err |= put_user(gentry.regnum, &g32->regnum);
297 err |= put_user(gentry.type, &g32->type);
298 break;
300 #endif
302 return err;
305 static int
306 mtrr_close(struct inode *ino, struct file *file)
308 int i, max;
309 unsigned int *fcount = FILE_FCOUNT(file);
311 if (fcount != NULL) {
312 max = num_var_ranges;
313 for (i = 0; i < max; ++i) {
314 while (fcount[i] > 0) {
315 mtrr_del(i, 0, 0);
316 --fcount[i];
319 kfree(fcount);
320 FILE_FCOUNT(file) = NULL;
322 return single_release(ino, file);
325 static int mtrr_seq_show(struct seq_file *seq, void *offset);
327 static int mtrr_open(struct inode *inode, struct file *file)
329 if (!mtrr_if)
330 return -EIO;
331 if (!mtrr_if->get)
332 return -ENXIO;
333 return single_open(file, mtrr_seq_show, NULL);
336 static struct file_operations mtrr_fops = {
337 .owner = THIS_MODULE,
338 .open = mtrr_open,
339 .read = seq_read,
340 .llseek = seq_lseek,
341 .write = mtrr_write,
342 .unlocked_ioctl = mtrr_ioctl,
343 .compat_ioctl = mtrr_ioctl,
344 .release = mtrr_close,
348 static struct proc_dir_entry *proc_root_mtrr;
351 static int mtrr_seq_show(struct seq_file *seq, void *offset)
353 char factor;
354 int i, max, len;
355 mtrr_type type;
356 unsigned long base;
357 unsigned int size;
359 len = 0;
360 max = num_var_ranges;
361 for (i = 0; i < max; i++) {
362 mtrr_if->get(i, &base, &size, &type);
363 if (size == 0)
364 usage_table[i] = 0;
365 else {
366 if (size < (0x100000 >> PAGE_SHIFT)) {
367 /* less than 1MB */
368 factor = 'K';
369 size <<= PAGE_SHIFT - 10;
370 } else {
371 factor = 'M';
372 size >>= 20 - PAGE_SHIFT;
374 /* RED-PEN: base can be > 32bit */
375 len += seq_printf(seq,
376 "reg%02i: base=0x%05lx000 (%4liMB), size=%4i%cB: %s, count=%d\n",
377 i, base, base >> (20 - PAGE_SHIFT), size, factor,
378 mtrr_attrib_to_str(type), usage_table[i]);
381 return 0;
384 static int __init mtrr_if_init(void)
386 struct cpuinfo_x86 *c = &boot_cpu_data;
388 if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
389 (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
390 (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
391 (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
392 return -ENODEV;
394 proc_root_mtrr =
395 create_proc_entry("mtrr", S_IWUSR | S_IRUGO, &proc_root);
396 if (proc_root_mtrr) {
397 proc_root_mtrr->owner = THIS_MODULE;
398 proc_root_mtrr->proc_fops = &mtrr_fops;
400 return 0;
403 arch_initcall(mtrr_if_init);
404 #endif /* CONFIG_PROC_FS */