Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Generic MTRR (Memory Type Range Register) driver. |
| 2 | |
| 3 | Copyright (C) 1997-2000 Richard Gooch |
| 4 | Copyright (c) 2002 Patrick Mochel |
| 5 | |
| 6 | This library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Library General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2 of the License, or (at your option) any later version. |
| 10 | |
| 11 | This library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Library General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Library General Public |
| 17 | License along with this library; if not, write to the Free |
| 18 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | |
| 20 | Richard Gooch may be reached by email at rgooch@atnf.csiro.au |
| 21 | The postal address is: |
| 22 | Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia. |
| 23 | |
| 24 | Source: "Pentium Pro Family Developer's Manual, Volume 3: |
| 25 | Operating System Writer's Guide" (Intel document number 242692), |
| 26 | section 11.11.7 |
| 27 | |
| 28 | This was cleaned and made readable by Patrick Mochel <mochel@osdl.org> |
| 29 | on 6-7 March 2002. |
| 30 | Source: Intel Architecture Software Developers Manual, Volume 3: |
| 31 | System Programming Guide; Section 9.11. (1997 edition - PPro). |
| 32 | */ |
| 33 | |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/pci.h> |
| 37 | #include <linux/smp.h> |
| 38 | #include <linux/cpu.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include <asm/mtrr.h> |
| 42 | |
| 43 | #include <asm/uaccess.h> |
| 44 | #include <asm/processor.h> |
| 45 | #include <asm/msr.h> |
| 46 | #include "mtrr.h" |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | u32 num_var_ranges = 0; |
| 49 | |
| 50 | unsigned int *usage_table; |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 51 | static DEFINE_MUTEX(mtrr_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | u32 size_or_mask, size_and_mask; |
| 54 | |
| 55 | static struct mtrr_ops * mtrr_ops[X86_VENDOR_NUM] = {}; |
| 56 | |
| 57 | struct mtrr_ops * mtrr_if = NULL; |
| 58 | |
| 59 | static void set_mtrr(unsigned int reg, unsigned long base, |
| 60 | unsigned long size, mtrr_type type); |
| 61 | |
Jan Beulich | 475850c | 2006-12-07 02:14:09 +0100 | [diff] [blame^] | 62 | #ifndef CONFIG_X86_64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | extern int arr3_protected; |
Jan Beulich | 475850c | 2006-12-07 02:14:09 +0100 | [diff] [blame^] | 64 | #else |
| 65 | #define arr3_protected 0 |
| 66 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | void set_mtrr_ops(struct mtrr_ops * ops) |
| 69 | { |
| 70 | if (ops->vendor && ops->vendor < X86_VENDOR_NUM) |
| 71 | mtrr_ops[ops->vendor] = ops; |
| 72 | } |
| 73 | |
| 74 | /* Returns non-zero if we have the write-combining memory type */ |
| 75 | static int have_wrcomb(void) |
| 76 | { |
| 77 | struct pci_dev *dev; |
Lee Revell | a6954ba | 2005-05-01 08:58:49 -0700 | [diff] [blame] | 78 | u8 rev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | if ((dev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL)) != NULL) { |
Lee Revell | a6954ba | 2005-05-01 08:58:49 -0700 | [diff] [blame] | 81 | /* ServerWorks LE chipsets < rev 6 have problems with write-combining |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | Don't allow it and leave room for other chipsets to be tagged */ |
| 83 | if (dev->vendor == PCI_VENDOR_ID_SERVERWORKS && |
| 84 | dev->device == PCI_DEVICE_ID_SERVERWORKS_LE) { |
Lee Revell | a6954ba | 2005-05-01 08:58:49 -0700 | [diff] [blame] | 85 | pci_read_config_byte(dev, PCI_CLASS_REVISION, &rev); |
| 86 | if (rev <= 5) { |
| 87 | printk(KERN_INFO "mtrr: Serverworks LE rev < 6 detected. Write-combining disabled.\n"); |
| 88 | pci_dev_put(dev); |
| 89 | return 0; |
| 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
Lee Revell | a6954ba | 2005-05-01 08:58:49 -0700 | [diff] [blame] | 92 | /* Intel 450NX errata # 23. Non ascending cacheline evictions to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | write combining memory may resulting in data corruption */ |
| 94 | if (dev->vendor == PCI_VENDOR_ID_INTEL && |
| 95 | dev->device == PCI_DEVICE_ID_INTEL_82451NX) { |
| 96 | printk(KERN_INFO "mtrr: Intel 450NX MMC detected. Write-combining disabled.\n"); |
| 97 | pci_dev_put(dev); |
| 98 | return 0; |
| 99 | } |
| 100 | pci_dev_put(dev); |
| 101 | } |
| 102 | return (mtrr_if->have_wrcomb ? mtrr_if->have_wrcomb() : 0); |
| 103 | } |
| 104 | |
| 105 | /* This function returns the number of variable MTRRs */ |
| 106 | static void __init set_num_var_ranges(void) |
| 107 | { |
| 108 | unsigned long config = 0, dummy; |
| 109 | |
| 110 | if (use_intel()) { |
| 111 | rdmsr(MTRRcap_MSR, config, dummy); |
| 112 | } else if (is_cpu(AMD)) |
| 113 | config = 2; |
| 114 | else if (is_cpu(CYRIX) || is_cpu(CENTAUR)) |
| 115 | config = 8; |
| 116 | num_var_ranges = config & 0xff; |
| 117 | } |
| 118 | |
| 119 | static void __init init_table(void) |
| 120 | { |
| 121 | int i, max; |
| 122 | |
| 123 | max = num_var_ranges; |
| 124 | if ((usage_table = kmalloc(max * sizeof *usage_table, GFP_KERNEL)) |
| 125 | == NULL) { |
| 126 | printk(KERN_ERR "mtrr: could not allocate\n"); |
| 127 | return; |
| 128 | } |
| 129 | for (i = 0; i < max; i++) |
| 130 | usage_table[i] = 1; |
| 131 | } |
| 132 | |
| 133 | struct set_mtrr_data { |
| 134 | atomic_t count; |
| 135 | atomic_t gate; |
| 136 | unsigned long smp_base; |
| 137 | unsigned long smp_size; |
| 138 | unsigned int smp_reg; |
| 139 | mtrr_type smp_type; |
| 140 | }; |
| 141 | |
| 142 | #ifdef CONFIG_SMP |
| 143 | |
| 144 | static void ipi_handler(void *info) |
| 145 | /* [SUMMARY] Synchronisation handler. Executed by "other" CPUs. |
| 146 | [RETURNS] Nothing. |
| 147 | */ |
| 148 | { |
| 149 | struct set_mtrr_data *data = info; |
| 150 | unsigned long flags; |
| 151 | |
| 152 | local_irq_save(flags); |
| 153 | |
| 154 | atomic_dec(&data->count); |
| 155 | while(!atomic_read(&data->gate)) |
| 156 | cpu_relax(); |
| 157 | |
| 158 | /* The master has cleared me to execute */ |
| 159 | if (data->smp_reg != ~0U) |
| 160 | mtrr_if->set(data->smp_reg, data->smp_base, |
| 161 | data->smp_size, data->smp_type); |
| 162 | else |
| 163 | mtrr_if->set_all(); |
| 164 | |
| 165 | atomic_dec(&data->count); |
| 166 | while(atomic_read(&data->gate)) |
| 167 | cpu_relax(); |
| 168 | |
| 169 | atomic_dec(&data->count); |
| 170 | local_irq_restore(flags); |
| 171 | } |
| 172 | |
| 173 | #endif |
| 174 | |
| 175 | /** |
| 176 | * set_mtrr - update mtrrs on all processors |
| 177 | * @reg: mtrr in question |
| 178 | * @base: mtrr base |
| 179 | * @size: mtrr size |
| 180 | * @type: mtrr type |
| 181 | * |
| 182 | * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly: |
| 183 | * |
| 184 | * 1. Send IPI to do the following: |
| 185 | * 2. Disable Interrupts |
| 186 | * 3. Wait for all procs to do so |
| 187 | * 4. Enter no-fill cache mode |
| 188 | * 5. Flush caches |
| 189 | * 6. Clear PGE bit |
| 190 | * 7. Flush all TLBs |
| 191 | * 8. Disable all range registers |
| 192 | * 9. Update the MTRRs |
| 193 | * 10. Enable all range registers |
| 194 | * 11. Flush all TLBs and caches again |
| 195 | * 12. Enter normal cache mode and reenable caching |
| 196 | * 13. Set PGE |
| 197 | * 14. Wait for buddies to catch up |
| 198 | * 15. Enable interrupts. |
| 199 | * |
| 200 | * What does that mean for us? Well, first we set data.count to the number |
| 201 | * of CPUs. As each CPU disables interrupts, it'll decrement it once. We wait |
| 202 | * until it hits 0 and proceed. We set the data.gate flag and reset data.count. |
| 203 | * Meanwhile, they are waiting for that flag to be set. Once it's set, each |
| 204 | * CPU goes through the transition of updating MTRRs. The CPU vendors may each do it |
| 205 | * differently, so we call mtrr_if->set() callback and let them take care of it. |
| 206 | * When they're done, they again decrement data->count and wait for data.gate to |
| 207 | * be reset. |
| 208 | * When we finish, we wait for data.count to hit 0 and toggle the data.gate flag. |
| 209 | * Everyone then enables interrupts and we all continue on. |
| 210 | * |
| 211 | * Note that the mechanism is the same for UP systems, too; all the SMP stuff |
| 212 | * becomes nops. |
| 213 | */ |
| 214 | static void set_mtrr(unsigned int reg, unsigned long base, |
| 215 | unsigned long size, mtrr_type type) |
| 216 | { |
| 217 | struct set_mtrr_data data; |
| 218 | unsigned long flags; |
| 219 | |
| 220 | data.smp_reg = reg; |
| 221 | data.smp_base = base; |
| 222 | data.smp_size = size; |
| 223 | data.smp_type = type; |
| 224 | atomic_set(&data.count, num_booting_cpus() - 1); |
| 225 | atomic_set(&data.gate,0); |
| 226 | |
| 227 | /* Start the ball rolling on other CPUs */ |
| 228 | if (smp_call_function(ipi_handler, &data, 1, 0) != 0) |
| 229 | panic("mtrr: timed out waiting for other CPUs\n"); |
| 230 | |
| 231 | local_irq_save(flags); |
| 232 | |
| 233 | while(atomic_read(&data.count)) |
| 234 | cpu_relax(); |
| 235 | |
| 236 | /* ok, reset count and toggle gate */ |
| 237 | atomic_set(&data.count, num_booting_cpus() - 1); |
| 238 | atomic_set(&data.gate,1); |
| 239 | |
| 240 | /* do our MTRR business */ |
| 241 | |
| 242 | /* HACK! |
| 243 | * We use this same function to initialize the mtrrs on boot. |
| 244 | * The state of the boot cpu's mtrrs has been saved, and we want |
| 245 | * to replicate across all the APs. |
| 246 | * If we're doing that @reg is set to something special... |
| 247 | */ |
| 248 | if (reg != ~0U) |
| 249 | mtrr_if->set(reg,base,size,type); |
| 250 | |
| 251 | /* wait for the others */ |
| 252 | while(atomic_read(&data.count)) |
| 253 | cpu_relax(); |
| 254 | |
| 255 | atomic_set(&data.count, num_booting_cpus() - 1); |
| 256 | atomic_set(&data.gate,0); |
| 257 | |
| 258 | /* |
| 259 | * Wait here for everyone to have seen the gate change |
| 260 | * So we're the last ones to touch 'data' |
| 261 | */ |
| 262 | while(atomic_read(&data.count)) |
| 263 | cpu_relax(); |
| 264 | |
| 265 | local_irq_restore(flags); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * mtrr_add_page - Add a memory type region |
Andreas Mohr | 9b48341 | 2006-12-07 02:14:00 +0100 | [diff] [blame] | 270 | * @base: Physical base address of region in pages (in units of 4 kB!) |
| 271 | * @size: Physical size of region in pages (4 kB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | * @type: Type of MTRR desired |
| 273 | * @increment: If this is true do usage counting on the region |
| 274 | * |
| 275 | * Memory type region registers control the caching on newer Intel and |
| 276 | * non Intel processors. This function allows drivers to request an |
| 277 | * MTRR is added. The details and hardware specifics of each processor's |
| 278 | * implementation are hidden from the caller, but nevertheless the |
| 279 | * caller should expect to need to provide a power of two size on an |
| 280 | * equivalent power of two boundary. |
| 281 | * |
| 282 | * If the region cannot be added either because all regions are in use |
| 283 | * or the CPU cannot support it a negative value is returned. On success |
| 284 | * the register number for this entry is returned, but should be treated |
| 285 | * as a cookie only. |
| 286 | * |
| 287 | * On a multiprocessor machine the changes are made to all processors. |
| 288 | * This is required on x86 by the Intel processors. |
| 289 | * |
| 290 | * The available types are |
| 291 | * |
| 292 | * %MTRR_TYPE_UNCACHABLE - No caching |
| 293 | * |
| 294 | * %MTRR_TYPE_WRBACK - Write data back in bursts whenever |
| 295 | * |
| 296 | * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts |
| 297 | * |
| 298 | * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes |
| 299 | * |
| 300 | * BUGS: Needs a quiet flag for the cases where drivers do not mind |
| 301 | * failures and do not wish system log messages to be sent. |
| 302 | */ |
| 303 | |
| 304 | int mtrr_add_page(unsigned long base, unsigned long size, |
| 305 | unsigned int type, char increment) |
| 306 | { |
| 307 | int i; |
| 308 | mtrr_type ltype; |
| 309 | unsigned long lbase; |
| 310 | unsigned int lsize; |
| 311 | int error; |
| 312 | |
| 313 | if (!mtrr_if) |
| 314 | return -ENXIO; |
| 315 | |
| 316 | if ((error = mtrr_if->validate_add_page(base,size,type))) |
| 317 | return error; |
| 318 | |
| 319 | if (type >= MTRR_NUM_TYPES) { |
| 320 | printk(KERN_WARNING "mtrr: type: %u invalid\n", type); |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | |
| 324 | /* If the type is WC, check that this processor supports it */ |
| 325 | if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) { |
| 326 | printk(KERN_WARNING |
| 327 | "mtrr: your processor doesn't support write-combining\n"); |
| 328 | return -ENOSYS; |
| 329 | } |
| 330 | |
| 331 | if (base & size_or_mask || size & size_or_mask) { |
| 332 | printk(KERN_WARNING "mtrr: base or size exceeds the MTRR width\n"); |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | |
| 336 | error = -EINVAL; |
| 337 | |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 338 | /* No CPU hotplug when we change MTRR entries */ |
| 339 | lock_cpu_hotplug(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | /* Search for existing MTRR */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 341 | mutex_lock(&mtrr_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | for (i = 0; i < num_var_ranges; ++i) { |
| 343 | mtrr_if->get(i, &lbase, &lsize, <ype); |
| 344 | if (base >= lbase + lsize) |
| 345 | continue; |
| 346 | if ((base < lbase) && (base + size <= lbase)) |
| 347 | continue; |
| 348 | /* At this point we know there is some kind of overlap/enclosure */ |
| 349 | if ((base < lbase) || (base + size > lbase + lsize)) { |
| 350 | printk(KERN_WARNING |
| 351 | "mtrr: 0x%lx000,0x%lx000 overlaps existing" |
| 352 | " 0x%lx000,0x%x000\n", base, size, lbase, |
| 353 | lsize); |
| 354 | goto out; |
| 355 | } |
| 356 | /* New region is enclosed by an existing region */ |
| 357 | if (ltype != type) { |
| 358 | if (type == MTRR_TYPE_UNCACHABLE) |
| 359 | continue; |
| 360 | printk (KERN_WARNING "mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n", |
| 361 | base, size, mtrr_attrib_to_str(ltype), |
| 362 | mtrr_attrib_to_str(type)); |
| 363 | goto out; |
| 364 | } |
| 365 | if (increment) |
| 366 | ++usage_table[i]; |
| 367 | error = i; |
| 368 | goto out; |
| 369 | } |
| 370 | /* Search for an empty MTRR */ |
| 371 | i = mtrr_if->get_free_region(base, size); |
| 372 | if (i >= 0) { |
| 373 | set_mtrr(i, base, size, type); |
| 374 | usage_table[i] = 1; |
| 375 | } else |
| 376 | printk(KERN_INFO "mtrr: no more MTRRs available\n"); |
| 377 | error = i; |
| 378 | out: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 379 | mutex_unlock(&mtrr_mutex); |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 380 | unlock_cpu_hotplug(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | return error; |
| 382 | } |
| 383 | |
Andrew Morton | c92c6ff | 2005-06-23 00:08:35 -0700 | [diff] [blame] | 384 | static int mtrr_check(unsigned long base, unsigned long size) |
| 385 | { |
| 386 | if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) { |
| 387 | printk(KERN_WARNING |
| 388 | "mtrr: size and base must be multiples of 4 kiB\n"); |
| 389 | printk(KERN_DEBUG |
| 390 | "mtrr: size: 0x%lx base: 0x%lx\n", size, base); |
| 391 | dump_stack(); |
| 392 | return -1; |
| 393 | } |
| 394 | return 0; |
| 395 | } |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | /** |
| 398 | * mtrr_add - Add a memory type region |
| 399 | * @base: Physical base address of region |
| 400 | * @size: Physical size of region |
| 401 | * @type: Type of MTRR desired |
| 402 | * @increment: If this is true do usage counting on the region |
| 403 | * |
| 404 | * Memory type region registers control the caching on newer Intel and |
| 405 | * non Intel processors. This function allows drivers to request an |
| 406 | * MTRR is added. The details and hardware specifics of each processor's |
| 407 | * implementation are hidden from the caller, but nevertheless the |
| 408 | * caller should expect to need to provide a power of two size on an |
| 409 | * equivalent power of two boundary. |
| 410 | * |
| 411 | * If the region cannot be added either because all regions are in use |
| 412 | * or the CPU cannot support it a negative value is returned. On success |
| 413 | * the register number for this entry is returned, but should be treated |
| 414 | * as a cookie only. |
| 415 | * |
| 416 | * On a multiprocessor machine the changes are made to all processors. |
| 417 | * This is required on x86 by the Intel processors. |
| 418 | * |
| 419 | * The available types are |
| 420 | * |
| 421 | * %MTRR_TYPE_UNCACHABLE - No caching |
| 422 | * |
| 423 | * %MTRR_TYPE_WRBACK - Write data back in bursts whenever |
| 424 | * |
| 425 | * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts |
| 426 | * |
| 427 | * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes |
| 428 | * |
| 429 | * BUGS: Needs a quiet flag for the cases where drivers do not mind |
| 430 | * failures and do not wish system log messages to be sent. |
| 431 | */ |
| 432 | |
| 433 | int |
| 434 | mtrr_add(unsigned long base, unsigned long size, unsigned int type, |
| 435 | char increment) |
| 436 | { |
Andrew Morton | c92c6ff | 2005-06-23 00:08:35 -0700 | [diff] [blame] | 437 | if (mtrr_check(base, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type, |
| 440 | increment); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * mtrr_del_page - delete a memory type region |
| 445 | * @reg: Register returned by mtrr_add |
| 446 | * @base: Physical base address |
| 447 | * @size: Size of region |
| 448 | * |
| 449 | * If register is supplied then base and size are ignored. This is |
| 450 | * how drivers should call it. |
| 451 | * |
| 452 | * Releases an MTRR region. If the usage count drops to zero the |
| 453 | * register is freed and the region returns to default state. |
| 454 | * On success the register is returned, on failure a negative error |
| 455 | * code. |
| 456 | */ |
| 457 | |
| 458 | int mtrr_del_page(int reg, unsigned long base, unsigned long size) |
| 459 | { |
| 460 | int i, max; |
| 461 | mtrr_type ltype; |
| 462 | unsigned long lbase; |
| 463 | unsigned int lsize; |
| 464 | int error = -EINVAL; |
| 465 | |
| 466 | if (!mtrr_if) |
| 467 | return -ENXIO; |
| 468 | |
| 469 | max = num_var_ranges; |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 470 | /* No CPU hotplug when we change MTRR entries */ |
| 471 | lock_cpu_hotplug(); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 472 | mutex_lock(&mtrr_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | if (reg < 0) { |
| 474 | /* Search for existing MTRR */ |
| 475 | for (i = 0; i < max; ++i) { |
| 476 | mtrr_if->get(i, &lbase, &lsize, <ype); |
| 477 | if (lbase == base && lsize == size) { |
| 478 | reg = i; |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | if (reg < 0) { |
| 483 | printk(KERN_DEBUG "mtrr: no MTRR for %lx000,%lx000 found\n", base, |
| 484 | size); |
| 485 | goto out; |
| 486 | } |
| 487 | } |
| 488 | if (reg >= max) { |
| 489 | printk(KERN_WARNING "mtrr: register: %d too big\n", reg); |
| 490 | goto out; |
| 491 | } |
| 492 | if (is_cpu(CYRIX) && !use_intel()) { |
| 493 | if ((reg == 3) && arr3_protected) { |
| 494 | printk(KERN_WARNING "mtrr: ARR3 cannot be changed\n"); |
| 495 | goto out; |
| 496 | } |
| 497 | } |
| 498 | mtrr_if->get(reg, &lbase, &lsize, <ype); |
| 499 | if (lsize < 1) { |
| 500 | printk(KERN_WARNING "mtrr: MTRR %d not used\n", reg); |
| 501 | goto out; |
| 502 | } |
| 503 | if (usage_table[reg] < 1) { |
| 504 | printk(KERN_WARNING "mtrr: reg: %d has count=0\n", reg); |
| 505 | goto out; |
| 506 | } |
| 507 | if (--usage_table[reg] < 1) |
| 508 | set_mtrr(reg, 0, 0, 0); |
| 509 | error = reg; |
| 510 | out: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 511 | mutex_unlock(&mtrr_mutex); |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 512 | unlock_cpu_hotplug(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | return error; |
| 514 | } |
| 515 | /** |
| 516 | * mtrr_del - delete a memory type region |
| 517 | * @reg: Register returned by mtrr_add |
| 518 | * @base: Physical base address |
| 519 | * @size: Size of region |
| 520 | * |
| 521 | * If register is supplied then base and size are ignored. This is |
| 522 | * how drivers should call it. |
| 523 | * |
| 524 | * Releases an MTRR region. If the usage count drops to zero the |
| 525 | * register is freed and the region returns to default state. |
| 526 | * On success the register is returned, on failure a negative error |
| 527 | * code. |
| 528 | */ |
| 529 | |
| 530 | int |
| 531 | mtrr_del(int reg, unsigned long base, unsigned long size) |
| 532 | { |
Andrew Morton | c92c6ff | 2005-06-23 00:08:35 -0700 | [diff] [blame] | 533 | if (mtrr_check(base, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT); |
| 536 | } |
| 537 | |
| 538 | EXPORT_SYMBOL(mtrr_add); |
| 539 | EXPORT_SYMBOL(mtrr_del); |
| 540 | |
| 541 | /* HACK ALERT! |
| 542 | * These should be called implicitly, but we can't yet until all the initcall |
| 543 | * stuff is done... |
| 544 | */ |
| 545 | extern void amd_init_mtrr(void); |
| 546 | extern void cyrix_init_mtrr(void); |
| 547 | extern void centaur_init_mtrr(void); |
| 548 | |
| 549 | static void __init init_ifs(void) |
| 550 | { |
Jan Beulich | 475850c | 2006-12-07 02:14:09 +0100 | [diff] [blame^] | 551 | #ifndef CONFIG_X86_64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | amd_init_mtrr(); |
| 553 | cyrix_init_mtrr(); |
| 554 | centaur_init_mtrr(); |
Jan Beulich | 475850c | 2006-12-07 02:14:09 +0100 | [diff] [blame^] | 555 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 558 | /* The suspend/resume methods are only for CPU without MTRR. CPU using generic |
| 559 | * MTRR driver doesn't require this |
| 560 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | struct mtrr_value { |
| 562 | mtrr_type ltype; |
| 563 | unsigned long lbase; |
| 564 | unsigned int lsize; |
| 565 | }; |
| 566 | |
| 567 | static struct mtrr_value * mtrr_state; |
| 568 | |
Pavel Machek | 829ca9a | 2005-09-03 15:56:56 -0700 | [diff] [blame] | 569 | static int mtrr_save(struct sys_device * sysdev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { |
| 571 | int i; |
| 572 | int size = num_var_ranges * sizeof(struct mtrr_value); |
| 573 | |
| 574 | mtrr_state = kmalloc(size,GFP_ATOMIC); |
| 575 | if (mtrr_state) |
| 576 | memset(mtrr_state,0,size); |
| 577 | else |
| 578 | return -ENOMEM; |
| 579 | |
| 580 | for (i = 0; i < num_var_ranges; i++) { |
| 581 | mtrr_if->get(i, |
| 582 | &mtrr_state[i].lbase, |
| 583 | &mtrr_state[i].lsize, |
| 584 | &mtrr_state[i].ltype); |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int mtrr_restore(struct sys_device * sysdev) |
| 590 | { |
| 591 | int i; |
| 592 | |
| 593 | for (i = 0; i < num_var_ranges; i++) { |
| 594 | if (mtrr_state[i].lsize) |
| 595 | set_mtrr(i, |
| 596 | mtrr_state[i].lbase, |
| 597 | mtrr_state[i].lsize, |
| 598 | mtrr_state[i].ltype); |
| 599 | } |
| 600 | kfree(mtrr_state); |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | |
| 606 | static struct sysdev_driver mtrr_sysdev_driver = { |
| 607 | .suspend = mtrr_save, |
| 608 | .resume = mtrr_restore, |
| 609 | }; |
| 610 | |
| 611 | |
| 612 | /** |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 613 | * mtrr_bp_init - initialize mtrrs on the boot CPU |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | * |
| 615 | * This needs to be called early; before any of the other CPUs are |
| 616 | * initialized (i.e. before smp_init()). |
| 617 | * |
| 618 | */ |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 619 | void __init mtrr_bp_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | { |
| 621 | init_ifs(); |
| 622 | |
| 623 | if (cpu_has_mtrr) { |
| 624 | mtrr_if = &generic_mtrr_ops; |
| 625 | size_or_mask = 0xff000000; /* 36 bits */ |
| 626 | size_and_mask = 0x00f00000; |
Andi Kleen | 1f2c958 | 2005-04-16 15:25:10 -0700 | [diff] [blame] | 627 | |
| 628 | /* This is an AMD specific MSR, but we assume(hope?) that |
| 629 | Intel will implement it to when they extend the address |
| 630 | bus of the Xeon. */ |
| 631 | if (cpuid_eax(0x80000000) >= 0x80000008) { |
| 632 | u32 phys_addr; |
| 633 | phys_addr = cpuid_eax(0x80000008) & 0xff; |
Shaohua Li | af9c142 | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 634 | /* CPUID workaround for Intel 0F33/0F34 CPU */ |
| 635 | if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && |
| 636 | boot_cpu_data.x86 == 0xF && |
| 637 | boot_cpu_data.x86_model == 0x3 && |
| 638 | (boot_cpu_data.x86_mask == 0x3 || |
| 639 | boot_cpu_data.x86_mask == 0x4)) |
| 640 | phys_addr = 36; |
| 641 | |
Andi Kleen | 1f2c958 | 2005-04-16 15:25:10 -0700 | [diff] [blame] | 642 | size_or_mask = ~((1 << (phys_addr - PAGE_SHIFT)) - 1); |
| 643 | size_and_mask = ~size_or_mask & 0xfff00000; |
| 644 | } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR && |
| 645 | boot_cpu_data.x86 == 6) { |
| 646 | /* VIA C* family have Intel style MTRRs, but |
| 647 | don't support PAE */ |
| 648 | size_or_mask = 0xfff00000; /* 32 bits */ |
| 649 | size_and_mask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | } else { |
| 652 | switch (boot_cpu_data.x86_vendor) { |
| 653 | case X86_VENDOR_AMD: |
| 654 | if (cpu_has_k6_mtrr) { |
| 655 | /* Pre-Athlon (K6) AMD CPU MTRRs */ |
| 656 | mtrr_if = mtrr_ops[X86_VENDOR_AMD]; |
| 657 | size_or_mask = 0xfff00000; /* 32 bits */ |
| 658 | size_and_mask = 0; |
| 659 | } |
| 660 | break; |
| 661 | case X86_VENDOR_CENTAUR: |
| 662 | if (cpu_has_centaur_mcr) { |
| 663 | mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR]; |
| 664 | size_or_mask = 0xfff00000; /* 32 bits */ |
| 665 | size_and_mask = 0; |
| 666 | } |
| 667 | break; |
| 668 | case X86_VENDOR_CYRIX: |
| 669 | if (cpu_has_cyrix_arr) { |
| 670 | mtrr_if = mtrr_ops[X86_VENDOR_CYRIX]; |
| 671 | size_or_mask = 0xfff00000; /* 32 bits */ |
| 672 | size_and_mask = 0; |
| 673 | } |
| 674 | break; |
| 675 | default: |
| 676 | break; |
| 677 | } |
| 678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
| 680 | if (mtrr_if) { |
| 681 | set_num_var_ranges(); |
| 682 | init_table(); |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 683 | if (use_intel()) |
| 684 | get_mtrr_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 688 | void mtrr_ap_init(void) |
| 689 | { |
| 690 | unsigned long flags; |
| 691 | |
| 692 | if (!mtrr_if || !use_intel()) |
| 693 | return; |
| 694 | /* |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 695 | * Ideally we should hold mtrr_mutex here to avoid mtrr entries changed, |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 696 | * but this routine will be called in cpu boot time, holding the lock |
| 697 | * breaks it. This routine is called in two cases: 1.very earily time |
| 698 | * of software resume, when there absolutely isn't mtrr entry changes; |
| 699 | * 2.cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug lock to |
| 700 | * prevent mtrr entry changes |
| 701 | */ |
| 702 | local_irq_save(flags); |
| 703 | |
| 704 | mtrr_if->set_all(); |
| 705 | |
| 706 | local_irq_restore(flags); |
| 707 | } |
| 708 | |
| 709 | static int __init mtrr_init_finialize(void) |
| 710 | { |
| 711 | if (!mtrr_if) |
| 712 | return 0; |
| 713 | if (use_intel()) |
| 714 | mtrr_state_warn(); |
| 715 | else { |
| 716 | /* The CPUs haven't MTRR and seemes not support SMP. They have |
| 717 | * specific drivers, we use a tricky method to support |
| 718 | * suspend/resume for them. |
| 719 | * TBD: is there any system with such CPU which supports |
| 720 | * suspend/resume? if no, we should remove the code. |
| 721 | */ |
| 722 | sysdev_driver_register(&cpu_sysdev_class, |
| 723 | &mtrr_sysdev_driver); |
| 724 | } |
| 725 | return 0; |
| 726 | } |
| 727 | subsys_initcall(mtrr_init_finialize); |