Merge branch 'i2c/for-4.18' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[linux-2.6-microblaze.git] / arch / x86 / kernel / cpu / mtrr / if.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/capability.h>
3 #include <linux/seq_file.h>
4 #include <linux/uaccess.h>
5 #include <linux/proc_fs.h>
6 #include <linux/ctype.h>
7 #include <linux/string.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10
11 #define LINE_SIZE 80
12
13 #include <asm/mtrr.h>
14
15 #include "mtrr.h"
16
17 #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
18
19 static const char *const mtrr_strings[MTRR_NUM_TYPES] =
20 {
21         "uncachable",           /* 0 */
22         "write-combining",      /* 1 */
23         "?",                    /* 2 */
24         "?",                    /* 3 */
25         "write-through",        /* 4 */
26         "write-protect",        /* 5 */
27         "write-back",           /* 6 */
28 };
29
30 const char *mtrr_attrib_to_str(int x)
31 {
32         return (x <= 6) ? mtrr_strings[x] : "?";
33 }
34
35 #ifdef CONFIG_PROC_FS
36
37 static int
38 mtrr_file_add(unsigned long base, unsigned long size,
39               unsigned int type, bool increment, struct file *file, int page)
40 {
41         unsigned int *fcount = FILE_FCOUNT(file);
42         int reg, max;
43
44         max = num_var_ranges;
45         if (fcount == NULL) {
46                 fcount = kcalloc(max, sizeof(*fcount), GFP_KERNEL);
47                 if (!fcount)
48                         return -ENOMEM;
49                 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         }
57         reg = mtrr_add_page(base, size, type, true);
58         if (reg >= 0)
59                 ++fcount[reg];
60         return reg;
61 }
62
63 static int
64 mtrr_file_del(unsigned long base, unsigned long size,
65               struct file *file, int page)
66 {
67         unsigned int *fcount = FILE_FCOUNT(file);
68         int reg;
69
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
87 /*
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  */
93 static ssize_t
94 mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
95 {
96         int i, err;
97         unsigned long reg;
98         unsigned long long base, size;
99         char *ptr;
100         char line[LINE_SIZE];
101         int length;
102         size_t linelen;
103
104         if (!capable(CAP_SYS_ADMIN))
105                 return -EPERM;
106
107         memset(line, 0, LINE_SIZE);
108
109         length = strncpy_from_user(line, buf, LINE_SIZE - 1);
110         if (length < 0)
111                 return length;
112
113         linelen = strlen(line);
114         ptr = line + linelen - 1;
115         if (linelen && *ptr == '\n')
116                 *ptr = '\0';
117
118         if (!strncmp(line, "disable=", 8)) {
119                 reg = simple_strtoul(line + 8, &ptr, 0);
120                 err = mtrr_del_page(reg, 0, 0);
121                 if (err < 0)
122                         return err;
123                 return len;
124         }
125
126         if (strncmp(line, "base=", 5))
127                 return -EINVAL;
128
129         base = simple_strtoull(line + 5, &ptr, 0);
130         ptr = skip_spaces(ptr);
131
132         if (strncmp(ptr, "size=", 5))
133                 return -EINVAL;
134
135         size = simple_strtoull(ptr + 5, &ptr, 0);
136         if ((base & 0xfff) || (size & 0xfff))
137                 return -EINVAL;
138         ptr = skip_spaces(ptr);
139
140         if (strncmp(ptr, "type=", 5))
141                 return -EINVAL;
142         ptr = skip_spaces(ptr + 5);
143
144         i = match_string(mtrr_strings, MTRR_NUM_TYPES, ptr);
145         if (i < 0)
146                 return i;
147
148         base >>= PAGE_SHIFT;
149         size >>= PAGE_SHIFT;
150         err = mtrr_add_page((unsigned long)base, (unsigned long)size, i, true);
151         if (err < 0)
152                 return err;
153         return len;
154 }
155
156 static long
157 mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
158 {
159         int err = 0;
160         mtrr_type type;
161         unsigned long base;
162         unsigned long size;
163         struct mtrr_sentry sentry;
164         struct mtrr_gentry gentry;
165         void __user *arg = (void __user *) __arg;
166
167         switch (cmd) {
168         case MTRRIOC_ADD_ENTRY:
169         case MTRRIOC_SET_ENTRY:
170         case MTRRIOC_DEL_ENTRY:
171         case MTRRIOC_KILL_ENTRY:
172         case MTRRIOC_ADD_PAGE_ENTRY:
173         case MTRRIOC_SET_PAGE_ENTRY:
174         case MTRRIOC_DEL_PAGE_ENTRY:
175         case MTRRIOC_KILL_PAGE_ENTRY:
176                 if (copy_from_user(&sentry, arg, sizeof sentry))
177                         return -EFAULT;
178                 break;
179         case MTRRIOC_GET_ENTRY:
180         case MTRRIOC_GET_PAGE_ENTRY:
181                 if (copy_from_user(&gentry, arg, sizeof gentry))
182                         return -EFAULT;
183                 break;
184 #ifdef CONFIG_COMPAT
185         case MTRRIOC32_ADD_ENTRY:
186         case MTRRIOC32_SET_ENTRY:
187         case MTRRIOC32_DEL_ENTRY:
188         case MTRRIOC32_KILL_ENTRY:
189         case MTRRIOC32_ADD_PAGE_ENTRY:
190         case MTRRIOC32_SET_PAGE_ENTRY:
191         case MTRRIOC32_DEL_PAGE_ENTRY:
192         case MTRRIOC32_KILL_PAGE_ENTRY: {
193                 struct mtrr_sentry32 __user *s32;
194
195                 s32 = (struct mtrr_sentry32 __user *)__arg;
196                 err = get_user(sentry.base, &s32->base);
197                 err |= get_user(sentry.size, &s32->size);
198                 err |= get_user(sentry.type, &s32->type);
199                 if (err)
200                         return err;
201                 break;
202         }
203         case MTRRIOC32_GET_ENTRY:
204         case MTRRIOC32_GET_PAGE_ENTRY: {
205                 struct mtrr_gentry32 __user *g32;
206
207                 g32 = (struct mtrr_gentry32 __user *)__arg;
208                 err = get_user(gentry.regnum, &g32->regnum);
209                 err |= get_user(gentry.base, &g32->base);
210                 err |= get_user(gentry.size, &g32->size);
211                 err |= get_user(gentry.type, &g32->type);
212                 if (err)
213                         return err;
214                 break;
215         }
216 #endif
217         }
218
219         switch (cmd) {
220         default:
221                 return -ENOTTY;
222         case MTRRIOC_ADD_ENTRY:
223 #ifdef CONFIG_COMPAT
224         case MTRRIOC32_ADD_ENTRY:
225 #endif
226                 if (!capable(CAP_SYS_ADMIN))
227                         return -EPERM;
228                 err =
229                     mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
230                                   file, 0);
231                 break;
232         case MTRRIOC_SET_ENTRY:
233 #ifdef CONFIG_COMPAT
234         case MTRRIOC32_SET_ENTRY:
235 #endif
236                 if (!capable(CAP_SYS_ADMIN))
237                         return -EPERM;
238                 err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
239                 break;
240         case MTRRIOC_DEL_ENTRY:
241 #ifdef CONFIG_COMPAT
242         case MTRRIOC32_DEL_ENTRY:
243 #endif
244                 if (!capable(CAP_SYS_ADMIN))
245                         return -EPERM;
246                 err = mtrr_file_del(sentry.base, sentry.size, file, 0);
247                 break;
248         case MTRRIOC_KILL_ENTRY:
249 #ifdef CONFIG_COMPAT
250         case MTRRIOC32_KILL_ENTRY:
251 #endif
252                 if (!capable(CAP_SYS_ADMIN))
253                         return -EPERM;
254                 err = mtrr_del(-1, sentry.base, sentry.size);
255                 break;
256         case MTRRIOC_GET_ENTRY:
257 #ifdef CONFIG_COMPAT
258         case MTRRIOC32_GET_ENTRY:
259 #endif
260                 if (gentry.regnum >= num_var_ranges)
261                         return -EINVAL;
262                 mtrr_if->get(gentry.regnum, &base, &size, &type);
263
264                 /* Hide entries that go above 4GB */
265                 if (base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
266                     || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
267                         gentry.base = gentry.size = gentry.type = 0;
268                 else {
269                         gentry.base = base << PAGE_SHIFT;
270                         gentry.size = size << PAGE_SHIFT;
271                         gentry.type = type;
272                 }
273
274                 break;
275         case MTRRIOC_ADD_PAGE_ENTRY:
276 #ifdef CONFIG_COMPAT
277         case MTRRIOC32_ADD_PAGE_ENTRY:
278 #endif
279                 if (!capable(CAP_SYS_ADMIN))
280                         return -EPERM;
281                 err =
282                     mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
283                                   file, 1);
284                 break;
285         case MTRRIOC_SET_PAGE_ENTRY:
286 #ifdef CONFIG_COMPAT
287         case MTRRIOC32_SET_PAGE_ENTRY:
288 #endif
289                 if (!capable(CAP_SYS_ADMIN))
290                         return -EPERM;
291                 err =
292                     mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
293                 break;
294         case MTRRIOC_DEL_PAGE_ENTRY:
295 #ifdef CONFIG_COMPAT
296         case MTRRIOC32_DEL_PAGE_ENTRY:
297 #endif
298                 if (!capable(CAP_SYS_ADMIN))
299                         return -EPERM;
300                 err = mtrr_file_del(sentry.base, sentry.size, file, 1);
301                 break;
302         case MTRRIOC_KILL_PAGE_ENTRY:
303 #ifdef CONFIG_COMPAT
304         case MTRRIOC32_KILL_PAGE_ENTRY:
305 #endif
306                 if (!capable(CAP_SYS_ADMIN))
307                         return -EPERM;
308                 err = mtrr_del_page(-1, sentry.base, sentry.size);
309                 break;
310         case MTRRIOC_GET_PAGE_ENTRY:
311 #ifdef CONFIG_COMPAT
312         case MTRRIOC32_GET_PAGE_ENTRY:
313 #endif
314                 if (gentry.regnum >= num_var_ranges)
315                         return -EINVAL;
316                 mtrr_if->get(gentry.regnum, &base, &size, &type);
317                 /* Hide entries that would overflow */
318                 if (size != (__typeof__(gentry.size))size)
319                         gentry.base = gentry.size = gentry.type = 0;
320                 else {
321                         gentry.base = base;
322                         gentry.size = size;
323                         gentry.type = type;
324                 }
325                 break;
326         }
327
328         if (err)
329                 return err;
330
331         switch (cmd) {
332         case MTRRIOC_GET_ENTRY:
333         case MTRRIOC_GET_PAGE_ENTRY:
334                 if (copy_to_user(arg, &gentry, sizeof gentry))
335                         err = -EFAULT;
336                 break;
337 #ifdef CONFIG_COMPAT
338         case MTRRIOC32_GET_ENTRY:
339         case MTRRIOC32_GET_PAGE_ENTRY: {
340                 struct mtrr_gentry32 __user *g32;
341
342                 g32 = (struct mtrr_gentry32 __user *)__arg;
343                 err = put_user(gentry.base, &g32->base);
344                 err |= put_user(gentry.size, &g32->size);
345                 err |= put_user(gentry.regnum, &g32->regnum);
346                 err |= put_user(gentry.type, &g32->type);
347                 break;
348         }
349 #endif
350         }
351         return err;
352 }
353
354 static int mtrr_close(struct inode *ino, struct file *file)
355 {
356         unsigned int *fcount = FILE_FCOUNT(file);
357         int i, max;
358
359         if (fcount != NULL) {
360                 max = num_var_ranges;
361                 for (i = 0; i < max; ++i) {
362                         while (fcount[i] > 0) {
363                                 mtrr_del(i, 0, 0);
364                                 --fcount[i];
365                         }
366                 }
367                 kfree(fcount);
368                 FILE_FCOUNT(file) = NULL;
369         }
370         return single_release(ino, file);
371 }
372
373 static int mtrr_seq_show(struct seq_file *seq, void *offset);
374
375 static int mtrr_open(struct inode *inode, struct file *file)
376 {
377         if (!mtrr_if)
378                 return -EIO;
379         if (!mtrr_if->get)
380                 return -ENXIO;
381         return single_open(file, mtrr_seq_show, NULL);
382 }
383
384 static const struct file_operations mtrr_fops = {
385         .owner                  = THIS_MODULE,
386         .open                   = mtrr_open,
387         .read                   = seq_read,
388         .llseek                 = seq_lseek,
389         .write                  = mtrr_write,
390         .unlocked_ioctl         = mtrr_ioctl,
391         .compat_ioctl           = mtrr_ioctl,
392         .release                = mtrr_close,
393 };
394
395 static int mtrr_seq_show(struct seq_file *seq, void *offset)
396 {
397         char factor;
398         int i, max;
399         mtrr_type type;
400         unsigned long base, size;
401
402         max = num_var_ranges;
403         for (i = 0; i < max; i++) {
404                 mtrr_if->get(i, &base, &size, &type);
405                 if (size == 0) {
406                         mtrr_usage_table[i] = 0;
407                         continue;
408                 }
409                 if (size < (0x100000 >> PAGE_SHIFT)) {
410                         /* less than 1MB */
411                         factor = 'K';
412                         size <<= PAGE_SHIFT - 10;
413                 } else {
414                         factor = 'M';
415                         size >>= 20 - PAGE_SHIFT;
416                 }
417                 /* Base can be > 32bit */
418                 seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
419                            i, base, base >> (20 - PAGE_SHIFT),
420                            size, factor,
421                            mtrr_usage_table[i], mtrr_attrib_to_str(type));
422         }
423         return 0;
424 }
425
426 static int __init mtrr_if_init(void)
427 {
428         struct cpuinfo_x86 *c = &boot_cpu_data;
429
430         if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
431             (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
432             (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
433             (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
434                 return -ENODEV;
435
436         proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_fops);
437         return 0;
438 }
439 arch_initcall(mtrr_if_init);
440 #endif                  /*  CONFIG_PROC_FS  */