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