blob: d259e5d2e0546adbec36d55f63b7f2a81f88e002 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Molnar14cc3e22006-03-26 01:37:14 -080039#include <linux/mutex.h>
Yinghai Lu95ffa242008-04-29 03:52:33 -070040#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jesse Barnes99fc8d42008-01-30 13:33:18 +010042#include <asm/e820.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/mtrr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/uaccess.h>
45#include <asm/processor.h>
46#include <asm/msr.h>
Joerg Roedel4147c872008-02-21 15:50:14 +010047#include <asm/kvm_para.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "mtrr.h"
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050u32 num_var_ranges = 0;
51
Sheng Yangb558bc02008-10-09 16:01:52 +080052unsigned int mtrr_usage_table[MTRR_MAX_VAR_RANGES];
Ingo Molnar14cc3e22006-03-26 01:37:14 -080053static DEFINE_MUTEX(mtrr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Andreas Herrmann6c5806c2007-02-13 13:26:23 +010055u64 size_or_mask, size_and_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static struct mtrr_ops * mtrr_ops[X86_VENDOR_NUM] = {};
58
59struct mtrr_ops * mtrr_if = NULL;
60
61static void set_mtrr(unsigned int reg, unsigned long base,
62 unsigned long size, mtrr_type type);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064void set_mtrr_ops(struct mtrr_ops * ops)
65{
66 if (ops->vendor && ops->vendor < X86_VENDOR_NUM)
67 mtrr_ops[ops->vendor] = ops;
68}
69
70/* Returns non-zero if we have the write-combining memory type */
71static int have_wrcomb(void)
72{
73 struct pci_dev *dev;
Lee Revella6954ba2005-05-01 08:58:49 -070074 u8 rev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if ((dev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, NULL)) != NULL) {
Lee Revella6954ba2005-05-01 08:58:49 -070077 /* ServerWorks LE chipsets < rev 6 have problems with write-combining
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 Don't allow it and leave room for other chipsets to be tagged */
79 if (dev->vendor == PCI_VENDOR_ID_SERVERWORKS &&
80 dev->device == PCI_DEVICE_ID_SERVERWORKS_LE) {
Lee Revella6954ba2005-05-01 08:58:49 -070081 pci_read_config_byte(dev, PCI_CLASS_REVISION, &rev);
82 if (rev <= 5) {
83 printk(KERN_INFO "mtrr: Serverworks LE rev < 6 detected. Write-combining disabled.\n");
84 pci_dev_put(dev);
85 return 0;
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Lee Revella6954ba2005-05-01 08:58:49 -070088 /* Intel 450NX errata # 23. Non ascending cacheline evictions to
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 write combining memory may resulting in data corruption */
90 if (dev->vendor == PCI_VENDOR_ID_INTEL &&
91 dev->device == PCI_DEVICE_ID_INTEL_82451NX) {
92 printk(KERN_INFO "mtrr: Intel 450NX MMC detected. Write-combining disabled.\n");
93 pci_dev_put(dev);
94 return 0;
95 }
96 pci_dev_put(dev);
97 }
98 return (mtrr_if->have_wrcomb ? mtrr_if->have_wrcomb() : 0);
99}
100
101/* This function returns the number of variable MTRRs */
102static void __init set_num_var_ranges(void)
103{
104 unsigned long config = 0, dummy;
105
106 if (use_intel()) {
107 rdmsr(MTRRcap_MSR, config, dummy);
108 } else if (is_cpu(AMD))
109 config = 2;
110 else if (is_cpu(CYRIX) || is_cpu(CENTAUR))
111 config = 8;
112 num_var_ranges = config & 0xff;
113}
114
115static void __init init_table(void)
116{
117 int i, max;
118
119 max = num_var_ranges;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 for (i = 0; i < max; i++)
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100121 mtrr_usage_table[i] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124struct set_mtrr_data {
125 atomic_t count;
126 atomic_t gate;
127 unsigned long smp_base;
128 unsigned long smp_size;
129 unsigned int smp_reg;
130 mtrr_type smp_type;
131};
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static void ipi_handler(void *info)
134/* [SUMMARY] Synchronisation handler. Executed by "other" CPUs.
135 [RETURNS] Nothing.
136*/
137{
Ingo Molnar4e2947f2007-11-09 22:39:38 +0100138#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct set_mtrr_data *data = info;
140 unsigned long flags;
141
142 local_irq_save(flags);
143
144 atomic_dec(&data->count);
145 while(!atomic_read(&data->gate))
146 cpu_relax();
147
148 /* The master has cleared me to execute */
149 if (data->smp_reg != ~0U)
150 mtrr_if->set(data->smp_reg, data->smp_base,
151 data->smp_size, data->smp_type);
152 else
153 mtrr_if->set_all();
154
155 atomic_dec(&data->count);
156 while(atomic_read(&data->gate))
157 cpu_relax();
158
159 atomic_dec(&data->count);
160 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#endif
Ingo Molnar4e2947f2007-11-09 22:39:38 +0100162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jan Beulich365bff82006-12-07 02:14:09 +0100164static inline int types_compatible(mtrr_type type1, mtrr_type type2) {
165 return type1 == MTRR_TYPE_UNCACHABLE ||
166 type2 == MTRR_TYPE_UNCACHABLE ||
167 (type1 == MTRR_TYPE_WRTHROUGH && type2 == MTRR_TYPE_WRBACK) ||
168 (type1 == MTRR_TYPE_WRBACK && type2 == MTRR_TYPE_WRTHROUGH);
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/**
172 * set_mtrr - update mtrrs on all processors
173 * @reg: mtrr in question
174 * @base: mtrr base
175 * @size: mtrr size
176 * @type: mtrr type
177 *
178 * This is kinda tricky, but fortunately, Intel spelled it out for us cleanly:
179 *
180 * 1. Send IPI to do the following:
181 * 2. Disable Interrupts
182 * 3. Wait for all procs to do so
183 * 4. Enter no-fill cache mode
184 * 5. Flush caches
185 * 6. Clear PGE bit
186 * 7. Flush all TLBs
187 * 8. Disable all range registers
188 * 9. Update the MTRRs
189 * 10. Enable all range registers
190 * 11. Flush all TLBs and caches again
191 * 12. Enter normal cache mode and reenable caching
192 * 13. Set PGE
193 * 14. Wait for buddies to catch up
194 * 15. Enable interrupts.
195 *
196 * What does that mean for us? Well, first we set data.count to the number
197 * of CPUs. As each CPU disables interrupts, it'll decrement it once. We wait
198 * until it hits 0 and proceed. We set the data.gate flag and reset data.count.
199 * Meanwhile, they are waiting for that flag to be set. Once it's set, each
200 * CPU goes through the transition of updating MTRRs. The CPU vendors may each do it
201 * differently, so we call mtrr_if->set() callback and let them take care of it.
202 * When they're done, they again decrement data->count and wait for data.gate to
203 * be reset.
204 * When we finish, we wait for data.count to hit 0 and toggle the data.gate flag.
205 * Everyone then enables interrupts and we all continue on.
206 *
207 * Note that the mechanism is the same for UP systems, too; all the SMP stuff
208 * becomes nops.
209 */
210static void set_mtrr(unsigned int reg, unsigned long base,
211 unsigned long size, mtrr_type type)
212{
213 struct set_mtrr_data data;
214 unsigned long flags;
215
216 data.smp_reg = reg;
217 data.smp_base = base;
218 data.smp_size = size;
219 data.smp_type = type;
220 atomic_set(&data.count, num_booting_cpus() - 1);
Loic Pryllid25c1ba2007-07-06 02:39:52 -0700221 /* make sure data.count is visible before unleashing other CPUs */
222 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 atomic_set(&data.gate,0);
224
225 /* Start the ball rolling on other CPUs */
Jens Axboe8691e5a2008-06-06 11:18:06 +0200226 if (smp_call_function(ipi_handler, &data, 0) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 panic("mtrr: timed out waiting for other CPUs\n");
228
229 local_irq_save(flags);
230
231 while(atomic_read(&data.count))
232 cpu_relax();
233
234 /* ok, reset count and toggle gate */
235 atomic_set(&data.count, num_booting_cpus() - 1);
Loic Pryllid25c1ba2007-07-06 02:39:52 -0700236 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 atomic_set(&data.gate,1);
238
239 /* do our MTRR business */
240
241 /* HACK!
242 * We use this same function to initialize the mtrrs on boot.
243 * The state of the boot cpu's mtrrs has been saved, and we want
244 * to replicate across all the APs.
245 * If we're doing that @reg is set to something special...
246 */
247 if (reg != ~0U)
248 mtrr_if->set(reg,base,size,type);
249
250 /* wait for the others */
251 while(atomic_read(&data.count))
252 cpu_relax();
253
254 atomic_set(&data.count, num_booting_cpus() - 1);
Loic Pryllid25c1ba2007-07-06 02:39:52 -0700255 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 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 Mohr9b483412006-12-07 02:14:00 +0100270 * @base: Physical base address of region in pages (in units of 4 kB!)
271 * @size: Physical size of region in pages (4 kB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * @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
304int mtrr_add_page(unsigned long base, unsigned long size,
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100305 unsigned int type, bool increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Jan Beulich365bff82006-12-07 02:14:09 +0100307 int i, replace, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 mtrr_type ltype;
Jan Beulich365bff82006-12-07 02:14:09 +0100309 unsigned long lbase, lsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 if (!mtrr_if)
312 return -ENXIO;
313
314 if ((error = mtrr_if->validate_add_page(base,size,type)))
315 return error;
316
317 if (type >= MTRR_NUM_TYPES) {
318 printk(KERN_WARNING "mtrr: type: %u invalid\n", type);
319 return -EINVAL;
320 }
321
322 /* If the type is WC, check that this processor supports it */
323 if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
324 printk(KERN_WARNING
325 "mtrr: your processor doesn't support write-combining\n");
326 return -ENOSYS;
327 }
328
Jan Beulich365bff82006-12-07 02:14:09 +0100329 if (!size) {
330 printk(KERN_WARNING "mtrr: zero sized request\n");
331 return -EINVAL;
332 }
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (base & size_or_mask || size & size_or_mask) {
335 printk(KERN_WARNING "mtrr: base or size exceeds the MTRR width\n");
336 return -EINVAL;
337 }
338
339 error = -EINVAL;
Jan Beulich365bff82006-12-07 02:14:09 +0100340 replace = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Shaohua Li3b520b22005-07-07 17:56:38 -0700342 /* No CPU hotplug when we change MTRR entries */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100343 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* Search for existing MTRR */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800345 mutex_lock(&mtrr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 for (i = 0; i < num_var_ranges; ++i) {
347 mtrr_if->get(i, &lbase, &lsize, &ltype);
Jan Beulich365bff82006-12-07 02:14:09 +0100348 if (!lsize || base > lbase + lsize - 1 || base + size - 1 < lbase)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 continue;
350 /* At this point we know there is some kind of overlap/enclosure */
Jan Beulich365bff82006-12-07 02:14:09 +0100351 if (base < lbase || base + size - 1 > lbase + lsize - 1) {
352 if (base <= lbase && base + size - 1 >= lbase + lsize - 1) {
353 /* New region encloses an existing region */
354 if (type == ltype) {
355 replace = replace == -1 ? i : -2;
356 continue;
357 }
358 else if (types_compatible(type, ltype))
359 continue;
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 printk(KERN_WARNING
362 "mtrr: 0x%lx000,0x%lx000 overlaps existing"
Jan Beulich365bff82006-12-07 02:14:09 +0100363 " 0x%lx000,0x%lx000\n", base, size, lbase,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 lsize);
365 goto out;
366 }
367 /* New region is enclosed by an existing region */
368 if (ltype != type) {
Jan Beulich365bff82006-12-07 02:14:09 +0100369 if (types_compatible(type, ltype))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 continue;
371 printk (KERN_WARNING "mtrr: type mismatch for %lx000,%lx000 old: %s new: %s\n",
372 base, size, mtrr_attrib_to_str(ltype),
373 mtrr_attrib_to_str(type));
374 goto out;
375 }
376 if (increment)
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100377 ++mtrr_usage_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 error = i;
379 goto out;
380 }
381 /* Search for an empty MTRR */
Jan Beulich365bff82006-12-07 02:14:09 +0100382 i = mtrr_if->get_free_region(base, size, replace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (i >= 0) {
384 set_mtrr(i, base, size, type);
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100385 if (likely(replace < 0)) {
386 mtrr_usage_table[i] = 1;
387 } else {
388 mtrr_usage_table[i] = mtrr_usage_table[replace];
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100389 if (increment)
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100390 mtrr_usage_table[i]++;
Jan Beulich365bff82006-12-07 02:14:09 +0100391 if (unlikely(replace != i)) {
392 set_mtrr(replace, 0, 0, 0);
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100393 mtrr_usage_table[replace] = 0;
Jan Beulich365bff82006-12-07 02:14:09 +0100394 }
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else
397 printk(KERN_INFO "mtrr: no more MTRRs available\n");
398 error = i;
399 out:
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800400 mutex_unlock(&mtrr_mutex);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100401 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return error;
403}
404
Andrew Mortonc92c6ff2005-06-23 00:08:35 -0700405static int mtrr_check(unsigned long base, unsigned long size)
406{
407 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
408 printk(KERN_WARNING
409 "mtrr: size and base must be multiples of 4 kiB\n");
410 printk(KERN_DEBUG
411 "mtrr: size: 0x%lx base: 0x%lx\n", size, base);
412 dump_stack();
413 return -1;
414 }
415 return 0;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * mtrr_add - Add a memory type region
420 * @base: Physical base address of region
421 * @size: Physical size of region
422 * @type: Type of MTRR desired
423 * @increment: If this is true do usage counting on the region
424 *
425 * Memory type region registers control the caching on newer Intel and
426 * non Intel processors. This function allows drivers to request an
427 * MTRR is added. The details and hardware specifics of each processor's
428 * implementation are hidden from the caller, but nevertheless the
429 * caller should expect to need to provide a power of two size on an
430 * equivalent power of two boundary.
431 *
432 * If the region cannot be added either because all regions are in use
433 * or the CPU cannot support it a negative value is returned. On success
434 * the register number for this entry is returned, but should be treated
435 * as a cookie only.
436 *
437 * On a multiprocessor machine the changes are made to all processors.
438 * This is required on x86 by the Intel processors.
439 *
440 * The available types are
441 *
442 * %MTRR_TYPE_UNCACHABLE - No caching
443 *
444 * %MTRR_TYPE_WRBACK - Write data back in bursts whenever
445 *
446 * %MTRR_TYPE_WRCOMB - Write data back soon but allow bursts
447 *
448 * %MTRR_TYPE_WRTHROUGH - Cache reads but not writes
449 *
450 * BUGS: Needs a quiet flag for the cases where drivers do not mind
451 * failures and do not wish system log messages to be sent.
452 */
453
454int
455mtrr_add(unsigned long base, unsigned long size, unsigned int type,
Paul Jimenez2d2ee8d2008-01-30 13:30:31 +0100456 bool increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Andrew Mortonc92c6ff2005-06-23 00:08:35 -0700458 if (mtrr_check(base, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return mtrr_add_page(base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
461 increment);
462}
463
464/**
465 * mtrr_del_page - delete a memory type region
466 * @reg: Register returned by mtrr_add
467 * @base: Physical base address
468 * @size: Size of region
469 *
470 * If register is supplied then base and size are ignored. This is
471 * how drivers should call it.
472 *
473 * Releases an MTRR region. If the usage count drops to zero the
474 * register is freed and the region returns to default state.
475 * On success the register is returned, on failure a negative error
476 * code.
477 */
478
479int mtrr_del_page(int reg, unsigned long base, unsigned long size)
480{
481 int i, max;
482 mtrr_type ltype;
Jan Beulich365bff82006-12-07 02:14:09 +0100483 unsigned long lbase, lsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int error = -EINVAL;
485
486 if (!mtrr_if)
487 return -ENXIO;
488
489 max = num_var_ranges;
Shaohua Li3b520b22005-07-07 17:56:38 -0700490 /* No CPU hotplug when we change MTRR entries */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100491 get_online_cpus();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800492 mutex_lock(&mtrr_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (reg < 0) {
494 /* Search for existing MTRR */
495 for (i = 0; i < max; ++i) {
496 mtrr_if->get(i, &lbase, &lsize, &ltype);
497 if (lbase == base && lsize == size) {
498 reg = i;
499 break;
500 }
501 }
502 if (reg < 0) {
503 printk(KERN_DEBUG "mtrr: no MTRR for %lx000,%lx000 found\n", base,
504 size);
505 goto out;
506 }
507 }
508 if (reg >= max) {
509 printk(KERN_WARNING "mtrr: register: %d too big\n", reg);
510 goto out;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 mtrr_if->get(reg, &lbase, &lsize, &ltype);
513 if (lsize < 1) {
514 printk(KERN_WARNING "mtrr: MTRR %d not used\n", reg);
515 goto out;
516 }
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100517 if (mtrr_usage_table[reg] < 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 printk(KERN_WARNING "mtrr: reg: %d has count=0\n", reg);
519 goto out;
520 }
Jesse Barnes99fc8d42008-01-30 13:33:18 +0100521 if (--mtrr_usage_table[reg] < 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 set_mtrr(reg, 0, 0, 0);
523 error = reg;
524 out:
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800525 mutex_unlock(&mtrr_mutex);
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100526 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return error;
528}
529/**
530 * mtrr_del - delete a memory type region
531 * @reg: Register returned by mtrr_add
532 * @base: Physical base address
533 * @size: Size of region
534 *
535 * If register is supplied then base and size are ignored. This is
536 * how drivers should call it.
537 *
538 * Releases an MTRR region. If the usage count drops to zero the
539 * register is freed and the region returns to default state.
540 * On success the register is returned, on failure a negative error
541 * code.
542 */
543
544int
545mtrr_del(int reg, unsigned long base, unsigned long size)
546{
Andrew Mortonc92c6ff2005-06-23 00:08:35 -0700547 if (mtrr_check(base, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return mtrr_del_page(reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
550}
551
552EXPORT_SYMBOL(mtrr_add);
553EXPORT_SYMBOL(mtrr_del);
554
555/* HACK ALERT!
556 * These should be called implicitly, but we can't yet until all the initcall
557 * stuff is done...
558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static void __init init_ifs(void)
560{
Jan Beulich475850c2006-12-07 02:14:09 +0100561#ifndef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 amd_init_mtrr();
563 cyrix_init_mtrr();
564 centaur_init_mtrr();
Jan Beulich475850c2006-12-07 02:14:09 +0100565#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Shaohua Li3b520b22005-07-07 17:56:38 -0700568/* The suspend/resume methods are only for CPU without MTRR. CPU using generic
569 * MTRR driver doesn't require this
570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571struct mtrr_value {
572 mtrr_type ltype;
573 unsigned long lbase;
Jan Beulich365bff82006-12-07 02:14:09 +0100574 unsigned long lsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575};
576
Sheng Yangb558bc02008-10-09 16:01:52 +0800577static struct mtrr_value mtrr_state[MTRR_MAX_VAR_RANGES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Pavel Machek829ca9a2005-09-03 15:56:56 -0700579static int mtrr_save(struct sys_device * sysdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 for (i = 0; i < num_var_ranges; i++) {
584 mtrr_if->get(i,
585 &mtrr_state[i].lbase,
586 &mtrr_state[i].lsize,
587 &mtrr_state[i].ltype);
588 }
589 return 0;
590}
591
592static int mtrr_restore(struct sys_device * sysdev)
593{
594 int i;
595
596 for (i = 0; i < num_var_ranges; i++) {
597 if (mtrr_state[i].lsize)
598 set_mtrr(i,
599 mtrr_state[i].lbase,
600 mtrr_state[i].lsize,
601 mtrr_state[i].ltype);
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604}
605
606
607
608static struct sysdev_driver mtrr_sysdev_driver = {
609 .suspend = mtrr_save,
610 .resume = mtrr_restore,
611};
612
Yinghai Luf5098d62008-04-29 20:25:58 -0700613/* should be related to MTRR_VAR_RANGES nums */
Yinghai Lu95ffa242008-04-29 03:52:33 -0700614#define RANGE_NUM 256
615
616struct res_range {
617 unsigned long start;
618 unsigned long end;
619};
620
Yinghai Luf5098d62008-04-29 20:25:58 -0700621static int __init
622add_range(struct res_range *range, int nr_range, unsigned long start,
623 unsigned long end)
624{
625 /* out of slots */
626 if (nr_range >= RANGE_NUM)
627 return nr_range;
628
629 range[nr_range].start = start;
630 range[nr_range].end = end;
631
632 nr_range++;
633
634 return nr_range;
635}
636
637static int __init
638add_range_with_merge(struct res_range *range, int nr_range, unsigned long start,
639 unsigned long end)
Yinghai Lu95ffa242008-04-29 03:52:33 -0700640{
641 int i;
642
Yinghai Lu95ffa242008-04-29 03:52:33 -0700643 /* try to merge it with old one */
644 for (i = 0; i < nr_range; i++) {
645 unsigned long final_start, final_end;
646 unsigned long common_start, common_end;
647
648 if (!range[i].end)
649 continue;
650
651 common_start = max(range[i].start, start);
652 common_end = min(range[i].end, end);
653 if (common_start > common_end + 1)
654 continue;
655
656 final_start = min(range[i].start, start);
657 final_end = max(range[i].end, end);
658
659 range[i].start = final_start;
660 range[i].end = final_end;
661 return nr_range;
662 }
663
Yinghai Lu95ffa242008-04-29 03:52:33 -0700664 /* need to add that */
Yinghai Luf5098d62008-04-29 20:25:58 -0700665 return add_range(range, nr_range, start, end);
Yinghai Lu95ffa242008-04-29 03:52:33 -0700666}
Yinghai Luf5098d62008-04-29 20:25:58 -0700667
668static void __init
669subtract_range(struct res_range *range, unsigned long start, unsigned long end)
Yinghai Lu95ffa242008-04-29 03:52:33 -0700670{
Yinghai Luf5098d62008-04-29 20:25:58 -0700671 int i, j;
Yinghai Lu95ffa242008-04-29 03:52:33 -0700672
673 for (j = 0; j < RANGE_NUM; j++) {
674 if (!range[j].end)
675 continue;
676
677 if (start <= range[j].start && end >= range[j].end) {
678 range[j].start = 0;
679 range[j].end = 0;
680 continue;
681 }
682
Yinghai Lu12031a62008-05-02 02:40:22 -0700683 if (start <= range[j].start && end < range[j].end &&
684 range[j].start < end + 1) {
Yinghai Lu95ffa242008-04-29 03:52:33 -0700685 range[j].start = end + 1;
686 continue;
687 }
688
689
Yinghai Lu12031a62008-05-02 02:40:22 -0700690 if (start > range[j].start && end >= range[j].end &&
691 range[j].end > start - 1) {
Yinghai Lu95ffa242008-04-29 03:52:33 -0700692 range[j].end = start - 1;
693 continue;
694 }
695
696 if (start > range[j].start && end < range[j].end) {
697 /* find the new spare */
698 for (i = 0; i < RANGE_NUM; i++) {
699 if (range[i].end == 0)
700 break;
701 }
702 if (i < RANGE_NUM) {
703 range[i].end = range[j].end;
704 range[i].start = end + 1;
705 } else {
706 printk(KERN_ERR "run of slot in ranges\n");
707 }
708 range[j].end = start - 1;
709 continue;
710 }
711 }
712}
713
714static int __init cmp_range(const void *x1, const void *x2)
715{
716 const struct res_range *r1 = x1;
717 const struct res_range *r2 = x2;
718 long start1, start2;
719
720 start1 = r1->start;
721 start2 = r2->start;
722
723 return start1 - start2;
724}
725
Yinghai Lu12031a62008-05-02 02:40:22 -0700726struct var_mtrr_range_state {
727 unsigned long base_pfn;
728 unsigned long size_pfn;
729 mtrr_type type;
730};
731
Jaswinder Singh1e849112008-07-21 22:58:29 +0530732static struct var_mtrr_range_state __initdata range_state[RANGE_NUM];
Yinghai Lu3f03c542008-05-09 22:40:52 -0700733static int __initdata debug_print;
Yinghai Lu12031a62008-05-02 02:40:22 -0700734
735static int __init
736x86_get_mtrr_mem_range(struct res_range *range, int nr_range,
737 unsigned long extra_remove_base,
738 unsigned long extra_remove_size)
739{
740 unsigned long i, base, size;
741 mtrr_type type;
742
743 for (i = 0; i < num_var_ranges; i++) {
744 type = range_state[i].type;
745 if (type != MTRR_TYPE_WRBACK)
746 continue;
747 base = range_state[i].base_pfn;
748 size = range_state[i].size_pfn;
749 nr_range = add_range_with_merge(range, nr_range, base,
750 base + size - 1);
751 }
Yinghai Lu3f03c542008-05-09 22:40:52 -0700752 if (debug_print) {
753 printk(KERN_DEBUG "After WB checking\n");
754 for (i = 0; i < nr_range; i++)
755 printk(KERN_DEBUG "MTRR MAP PFN: %016lx - %016lx\n",
Yinghai Lu12031a62008-05-02 02:40:22 -0700756 range[i].start, range[i].end + 1);
Yinghai Lu3f03c542008-05-09 22:40:52 -0700757 }
Yinghai Lu12031a62008-05-02 02:40:22 -0700758
759 /* take out UC ranges */
760 for (i = 0; i < num_var_ranges; i++) {
761 type = range_state[i].type;
Yinghai Ludd552352008-10-04 14:50:33 -0700762 if (type != MTRR_TYPE_UNCACHABLE &&
763 type != MTRR_TYPE_WRPROT)
Yinghai Lu12031a62008-05-02 02:40:22 -0700764 continue;
765 size = range_state[i].size_pfn;
766 if (!size)
767 continue;
768 base = range_state[i].base_pfn;
769 subtract_range(range, base, base + size - 1);
770 }
771 if (extra_remove_size)
772 subtract_range(range, extra_remove_base,
773 extra_remove_base + extra_remove_size - 1);
774
775 /* get new range num */
776 nr_range = 0;
777 for (i = 0; i < RANGE_NUM; i++) {
778 if (!range[i].end)
779 continue;
780 nr_range++;
781 }
Yinghai Lu3f03c542008-05-09 22:40:52 -0700782 if (debug_print) {
783 printk(KERN_DEBUG "After UC checking\n");
784 for (i = 0; i < nr_range; i++)
785 printk(KERN_DEBUG "MTRR MAP PFN: %016lx - %016lx\n",
786 range[i].start, range[i].end + 1);
787 }
Yinghai Lu12031a62008-05-02 02:40:22 -0700788
789 /* sort the ranges */
790 sort(range, nr_range, sizeof(struct res_range), cmp_range, NULL);
Yinghai Lu3f03c542008-05-09 22:40:52 -0700791 if (debug_print) {
792 printk(KERN_DEBUG "After sorting\n");
793 for (i = 0; i < nr_range; i++)
794 printk(KERN_DEBUG "MTRR MAP PFN: %016lx - %016lx\n",
Yinghai Lu12031a62008-05-02 02:40:22 -0700795 range[i].start, range[i].end + 1);
Yinghai Lu3f03c542008-05-09 22:40:52 -0700796 }
Yinghai Lu12031a62008-05-02 02:40:22 -0700797
798 /* clear those is not used */
799 for (i = nr_range; i < RANGE_NUM; i++)
800 memset(&range[i], 0, sizeof(range[i]));
801
802 return nr_range;
803}
804
805static struct res_range __initdata range[RANGE_NUM];
Yinghai Lu30604bb2008-10-14 18:59:18 -0700806static int __initdata nr_range;
Yinghai Lu12031a62008-05-02 02:40:22 -0700807
808#ifdef CONFIG_MTRR_SANITIZER
809
810static unsigned long __init sum_ranges(struct res_range *range, int nr_range)
811{
812 unsigned long sum;
813 int i;
814
815 sum = 0;
816 for (i = 0; i < nr_range; i++)
817 sum += range[i].end + 1 - range[i].start;
818
819 return sum;
820}
821
822static int enable_mtrr_cleanup __initdata =
823 CONFIG_MTRR_SANITIZER_ENABLE_DEFAULT;
824
825static int __init disable_mtrr_cleanup_setup(char *str)
826{
Rusty Russell71ab6b22008-12-25 17:18:43 +1030827 enable_mtrr_cleanup = 0;
Yinghai Lu12031a62008-05-02 02:40:22 -0700828 return 0;
829}
830early_param("disable_mtrr_cleanup", disable_mtrr_cleanup_setup);
831
832static int __init enable_mtrr_cleanup_setup(char *str)
833{
Rusty Russell71ab6b22008-12-25 17:18:43 +1030834 enable_mtrr_cleanup = 1;
Yinghai Lu12031a62008-05-02 02:40:22 -0700835 return 0;
836}
J.A. Magallón3dcd7e22008-09-30 10:02:52 +0200837early_param("enable_mtrr_cleanup", enable_mtrr_cleanup_setup);
Yinghai Lu12031a62008-05-02 02:40:22 -0700838
Yinghai Lu54d45ff2008-09-27 00:30:06 -0700839static int __init mtrr_cleanup_debug_setup(char *str)
840{
841 debug_print = 1;
842 return 0;
843}
844early_param("mtrr_cleanup_debug", mtrr_cleanup_debug_setup);
Yinghai Lu95ffa242008-04-29 03:52:33 -0700845
846struct var_mtrr_state {
Yinghai Luf5098d62008-04-29 20:25:58 -0700847 unsigned long range_startk;
848 unsigned long range_sizek;
849 unsigned long chunk_sizek;
850 unsigned long gran_sizek;
851 unsigned int reg;
Yinghai Lu95ffa242008-04-29 03:52:33 -0700852};
853
Yinghai Luf5098d62008-04-29 20:25:58 -0700854static void __init
855set_var_mtrr(unsigned int reg, unsigned long basek, unsigned long sizek,
Yinghai Lu12031a62008-05-02 02:40:22 -0700856 unsigned char type, unsigned int address_bits)
Yinghai Lu95ffa242008-04-29 03:52:33 -0700857{
858 u32 base_lo, base_hi, mask_lo, mask_hi;
Yinghai Luf5098d62008-04-29 20:25:58 -0700859 u64 base, mask;
Yinghai Lu95ffa242008-04-29 03:52:33 -0700860
861 if (!sizek) {
862 fill_mtrr_var_range(reg, 0, 0, 0, 0);
863 return;
864 }
865
Yinghai Luf5098d62008-04-29 20:25:58 -0700866 mask = (1ULL << address_bits) - 1;
867 mask &= ~((((u64)sizek) << 10) - 1);
Yinghai Lu95ffa242008-04-29 03:52:33 -0700868
Yinghai Luf5098d62008-04-29 20:25:58 -0700869 base = ((u64)basek) << 10;
Yinghai Lu95ffa242008-04-29 03:52:33 -0700870
Yinghai Luf5098d62008-04-29 20:25:58 -0700871 base |= type;
872 mask |= 0x800;
Yinghai Lu95ffa242008-04-29 03:52:33 -0700873
Yinghai Luf5098d62008-04-29 20:25:58 -0700874 base_lo = base & ((1ULL<<32) - 1);
875 base_hi = base >> 32;
876
877 mask_lo = mask & ((1ULL<<32) - 1);
878 mask_hi = mask >> 32;
879
Yinghai Lu95ffa242008-04-29 03:52:33 -0700880 fill_mtrr_var_range(reg, base_lo, base_hi, mask_lo, mask_hi);
881}
882
Yinghai Lu12031a62008-05-02 02:40:22 -0700883static void __init
884save_var_mtrr(unsigned int reg, unsigned long basek, unsigned long sizek,
885 unsigned char type)
886{
887 range_state[reg].base_pfn = basek >> (PAGE_SHIFT - 10);
888 range_state[reg].size_pfn = sizek >> (PAGE_SHIFT - 10);
889 range_state[reg].type = type;
890}
891
892static void __init
893set_var_mtrr_all(unsigned int address_bits)
894{
895 unsigned long basek, sizek;
896 unsigned char type;
897 unsigned int reg;
898
899 for (reg = 0; reg < num_var_ranges; reg++) {
900 basek = range_state[reg].base_pfn << (PAGE_SHIFT - 10);
901 sizek = range_state[reg].size_pfn << (PAGE_SHIFT - 10);
902 type = range_state[reg].type;
903
904 set_var_mtrr(reg, basek, sizek, type, address_bits);
905 }
906}
907
Yinghai Ludd7e5222008-09-29 18:54:11 -0700908static unsigned long to_size_factor(unsigned long sizek, char *factorp)
909{
910 char factor;
911 unsigned long base = sizek;
912
913 if (base & ((1<<10) - 1)) {
914 /* not MB alignment */
915 factor = 'K';
916 } else if (base & ((1<<20) - 1)){
917 factor = 'M';
918 base >>= 10;
919 } else {
920 factor = 'G';
921 base >>= 20;
922 }
923
924 *factorp = factor;
925
926 return base;
927}
928
Yinghai Luf5098d62008-04-29 20:25:58 -0700929static unsigned int __init
930range_to_mtrr(unsigned int reg, unsigned long range_startk,
Yinghai Lu12031a62008-05-02 02:40:22 -0700931 unsigned long range_sizek, unsigned char type)
Yinghai Lu95ffa242008-04-29 03:52:33 -0700932{
933 if (!range_sizek || (reg >= num_var_ranges))
934 return reg;
935
936 while (range_sizek) {
937 unsigned long max_align, align;
938 unsigned long sizek;
Yinghai Luf5098d62008-04-29 20:25:58 -0700939
Yinghai Lu95ffa242008-04-29 03:52:33 -0700940 /* Compute the maximum size I can make a range */
941 if (range_startk)
942 max_align = ffs(range_startk) - 1;
943 else
944 max_align = 32;
945 align = fls(range_sizek) - 1;
946 if (align > max_align)
947 align = max_align;
948
949 sizek = 1 << align;
Yinghai Ludd7e5222008-09-29 18:54:11 -0700950 if (debug_print) {
951 char start_factor = 'K', size_factor = 'K';
952 unsigned long start_base, size_base;
953
954 start_base = to_size_factor(range_startk, &start_factor),
955 size_base = to_size_factor(sizek, &size_factor),
956
Yinghai Lu3f03c542008-05-09 22:40:52 -0700957 printk(KERN_DEBUG "Setting variable MTRR %d, "
Yinghai Ludd7e5222008-09-29 18:54:11 -0700958 "base: %ld%cB, range: %ld%cB, type %s\n",
959 reg, start_base, start_factor,
960 size_base, size_factor,
Yinghai Lu3f03c542008-05-09 22:40:52 -0700961 (type == MTRR_TYPE_UNCACHABLE)?"UC":
962 ((type == MTRR_TYPE_WRBACK)?"WB":"Other")
963 );
Yinghai Ludd7e5222008-09-29 18:54:11 -0700964 }
Yinghai Lu12031a62008-05-02 02:40:22 -0700965 save_var_mtrr(reg++, range_startk, sizek, type);
Yinghai Lu95ffa242008-04-29 03:52:33 -0700966 range_startk += sizek;
967 range_sizek -= sizek;
968 if (reg >= num_var_ranges)
969 break;
970 }
971 return reg;
972}
973
Yinghai Lu12031a62008-05-02 02:40:22 -0700974static unsigned __init
975range_to_mtrr_with_hole(struct var_mtrr_state *state, unsigned long basek,
976 unsigned long sizek)
Yinghai Lu95ffa242008-04-29 03:52:33 -0700977{
978 unsigned long hole_basek, hole_sizek;
Yinghai Lu12031a62008-05-02 02:40:22 -0700979 unsigned long second_basek, second_sizek;
Yinghai Lu95ffa242008-04-29 03:52:33 -0700980 unsigned long range0_basek, range0_sizek;
981 unsigned long range_basek, range_sizek;
982 unsigned long chunk_sizek;
983 unsigned long gran_sizek;
984
985 hole_basek = 0;
986 hole_sizek = 0;
Yinghai Lu12031a62008-05-02 02:40:22 -0700987 second_basek = 0;
988 second_sizek = 0;
Yinghai Lu95ffa242008-04-29 03:52:33 -0700989 chunk_sizek = state->chunk_sizek;
990 gran_sizek = state->gran_sizek;
991
992 /* align with gran size, prevent small block used up MTRRs */
993 range_basek = ALIGN(state->range_startk, gran_sizek);
994 if ((range_basek > basek) && basek)
Yinghai Lu12031a62008-05-02 02:40:22 -0700995 return second_sizek;
996 state->range_sizek -= (range_basek - state->range_startk);
997 range_sizek = ALIGN(state->range_sizek, gran_sizek);
Yinghai Lu95ffa242008-04-29 03:52:33 -0700998
Yinghai Lu12031a62008-05-02 02:40:22 -0700999 while (range_sizek > state->range_sizek) {
Yinghai Lu95ffa242008-04-29 03:52:33 -07001000 range_sizek -= gran_sizek;
1001 if (!range_sizek)
Yinghai Lu12031a62008-05-02 02:40:22 -07001002 return 0;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001003 }
Yinghai Lu95ffa242008-04-29 03:52:33 -07001004 state->range_sizek = range_sizek;
1005
1006 /* try to append some small hole */
1007 range0_basek = state->range_startk;
1008 range0_sizek = ALIGN(state->range_sizek, chunk_sizek);
Yinghai Lu2313c272008-09-27 00:30:08 -07001009
1010 /* no increase */
Yinghai Luf5098d62008-04-29 20:25:58 -07001011 if (range0_sizek == state->range_sizek) {
Yinghai Lu3f03c542008-05-09 22:40:52 -07001012 if (debug_print)
1013 printk(KERN_DEBUG "rangeX: %016lx - %016lx\n",
1014 range0_basek<<10,
Yinghai Lu12031a62008-05-02 02:40:22 -07001015 (range0_basek + state->range_sizek)<<10);
1016 state->reg = range_to_mtrr(state->reg, range0_basek,
1017 state->range_sizek, MTRR_TYPE_WRBACK);
1018 return 0;
1019 }
1020
Yinghai Lu2313c272008-09-27 00:30:08 -07001021 /* only cut back, when it is not the last */
1022 if (sizek) {
1023 while (range0_basek + range0_sizek > (basek + sizek)) {
Yinghai Lu8f0afaa2008-09-27 20:26:06 -07001024 if (range0_sizek >= chunk_sizek)
1025 range0_sizek -= chunk_sizek;
1026 else
1027 range0_sizek = 0;
1028
Yinghai Lu2313c272008-09-27 00:30:08 -07001029 if (!range0_sizek)
1030 break;
1031 }
Yinghai Lu95ffa242008-04-29 03:52:33 -07001032 }
1033
Yinghai Lu8f0afaa2008-09-27 20:26:06 -07001034second_try:
1035 range_basek = range0_basek + range0_sizek;
1036
1037 /* one hole in the middle */
1038 if (range_basek > basek && range_basek <= (basek + sizek))
1039 second_sizek = range_basek - basek;
1040
1041 if (range0_sizek > state->range_sizek) {
1042
1043 /* one hole in middle or at end */
1044 hole_sizek = range0_sizek - state->range_sizek - second_sizek;
1045
1046 /* hole size should be less than half of range0 size */
Yinghai Lu834836e2008-10-02 15:46:20 -07001047 if (hole_sizek >= (range0_sizek >> 1) &&
Yinghai Lu8f0afaa2008-09-27 20:26:06 -07001048 range0_sizek >= chunk_sizek) {
1049 range0_sizek -= chunk_sizek;
1050 second_sizek = 0;
1051 hole_sizek = 0;
1052
1053 goto second_try;
1054 }
Yinghai Lu95ffa242008-04-29 03:52:33 -07001055 }
1056
Yinghai Lu12031a62008-05-02 02:40:22 -07001057 if (range0_sizek) {
Yinghai Lu3f03c542008-05-09 22:40:52 -07001058 if (debug_print)
1059 printk(KERN_DEBUG "range0: %016lx - %016lx\n",
1060 range0_basek<<10,
Yinghai Lu12031a62008-05-02 02:40:22 -07001061 (range0_basek + range0_sizek)<<10);
1062 state->reg = range_to_mtrr(state->reg, range0_basek,
1063 range0_sizek, MTRR_TYPE_WRBACK);
Yinghai Lu12031a62008-05-02 02:40:22 -07001064 }
Yinghai Lu95ffa242008-04-29 03:52:33 -07001065
Yinghai Lu8f0afaa2008-09-27 20:26:06 -07001066 if (range0_sizek < state->range_sizek) {
Yinghai Lu2313c272008-09-27 00:30:08 -07001067 /* need to handle left over */
Yinghai Lu12031a62008-05-02 02:40:22 -07001068 range_sizek = state->range_sizek - range0_sizek;
Yinghai Lu12031a62008-05-02 02:40:22 -07001069
Yinghai Lu8f0afaa2008-09-27 20:26:06 -07001070 if (debug_print)
1071 printk(KERN_DEBUG "range: %016lx - %016lx\n",
1072 range_basek<<10,
1073 (range_basek + range_sizek)<<10);
1074 state->reg = range_to_mtrr(state->reg, range_basek,
1075 range_sizek, MTRR_TYPE_WRBACK);
Yinghai Lu12031a62008-05-02 02:40:22 -07001076 }
1077
Yinghai Lu12031a62008-05-02 02:40:22 -07001078 if (hole_sizek) {
Yinghai Lu8f0afaa2008-09-27 20:26:06 -07001079 hole_basek = range_basek - hole_sizek - second_sizek;
Yinghai Lu3f03c542008-05-09 22:40:52 -07001080 if (debug_print)
1081 printk(KERN_DEBUG "hole: %016lx - %016lx\n",
Yinghai Lu8f0afaa2008-09-27 20:26:06 -07001082 hole_basek<<10,
1083 (hole_basek + hole_sizek)<<10);
1084 state->reg = range_to_mtrr(state->reg, hole_basek,
1085 hole_sizek, MTRR_TYPE_UNCACHABLE);
Yinghai Lu12031a62008-05-02 02:40:22 -07001086 }
1087
1088 return second_sizek;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001089}
1090
Yinghai Luf5098d62008-04-29 20:25:58 -07001091static void __init
1092set_var_mtrr_range(struct var_mtrr_state *state, unsigned long base_pfn,
1093 unsigned long size_pfn)
Yinghai Lu95ffa242008-04-29 03:52:33 -07001094{
1095 unsigned long basek, sizek;
Yinghai Lu12031a62008-05-02 02:40:22 -07001096 unsigned long second_sizek = 0;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001097
1098 if (state->reg >= num_var_ranges)
1099 return;
1100
1101 basek = base_pfn << (PAGE_SHIFT - 10);
1102 sizek = size_pfn << (PAGE_SHIFT - 10);
1103
1104 /* See if I can merge with the last range */
Yinghai Lu12031a62008-05-02 02:40:22 -07001105 if ((basek <= 1024) ||
1106 (state->range_startk + state->range_sizek == basek)) {
Yinghai Lu95ffa242008-04-29 03:52:33 -07001107 unsigned long endk = basek + sizek;
1108 state->range_sizek = endk - state->range_startk;
1109 return;
1110 }
1111 /* Write the range mtrrs */
Yinghai Lu12031a62008-05-02 02:40:22 -07001112 if (state->range_sizek != 0)
1113 second_sizek = range_to_mtrr_with_hole(state, basek, sizek);
Yinghai Lu95ffa242008-04-29 03:52:33 -07001114
Yinghai Lu95ffa242008-04-29 03:52:33 -07001115 /* Allocate an msr */
Yinghai Lu12031a62008-05-02 02:40:22 -07001116 state->range_startk = basek + second_sizek;
1117 state->range_sizek = sizek - second_sizek;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001118}
1119
1120/* mininum size of mtrr block that can take hole */
1121static u64 mtrr_chunk_size __initdata = (256ULL<<20);
1122
1123static int __init parse_mtrr_chunk_size_opt(char *p)
1124{
1125 if (!p)
1126 return -EINVAL;
1127 mtrr_chunk_size = memparse(p, &p);
1128 return 0;
1129}
1130early_param("mtrr_chunk_size", parse_mtrr_chunk_size_opt);
1131
1132/* granity of mtrr of block */
Yinghai Lu12031a62008-05-02 02:40:22 -07001133static u64 mtrr_gran_size __initdata;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001134
1135static int __init parse_mtrr_gran_size_opt(char *p)
1136{
1137 if (!p)
1138 return -EINVAL;
1139 mtrr_gran_size = memparse(p, &p);
1140 return 0;
1141}
1142early_param("mtrr_gran_size", parse_mtrr_gran_size_opt);
1143
Yinghai Lu12031a62008-05-02 02:40:22 -07001144static int nr_mtrr_spare_reg __initdata =
1145 CONFIG_MTRR_SANITIZER_SPARE_REG_NR_DEFAULT;
1146
1147static int __init parse_mtrr_spare_reg(char *arg)
1148{
1149 if (arg)
1150 nr_mtrr_spare_reg = simple_strtoul(arg, NULL, 0);
1151 return 0;
1152}
1153
1154early_param("mtrr_spare_reg_nr", parse_mtrr_spare_reg);
1155
1156static int __init
Yinghai Luf5098d62008-04-29 20:25:58 -07001157x86_setup_var_mtrrs(struct res_range *range, int nr_range,
Yinghai Lu12031a62008-05-02 02:40:22 -07001158 u64 chunk_size, u64 gran_size)
Yinghai Lu95ffa242008-04-29 03:52:33 -07001159{
1160 struct var_mtrr_state var_state;
1161 int i;
Yinghai Lu12031a62008-05-02 02:40:22 -07001162 int num_reg;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001163
Yinghai Luf5098d62008-04-29 20:25:58 -07001164 var_state.range_startk = 0;
1165 var_state.range_sizek = 0;
1166 var_state.reg = 0;
Yinghai Lu12031a62008-05-02 02:40:22 -07001167 var_state.chunk_sizek = chunk_size >> 10;
1168 var_state.gran_sizek = gran_size >> 10;
1169
1170 memset(range_state, 0, sizeof(range_state));
Yinghai Lu95ffa242008-04-29 03:52:33 -07001171
1172 /* Write the range etc */
1173 for (i = 0; i < nr_range; i++)
Yinghai Lu12031a62008-05-02 02:40:22 -07001174 set_var_mtrr_range(&var_state, range[i].start,
1175 range[i].end - range[i].start + 1);
Yinghai Lu95ffa242008-04-29 03:52:33 -07001176
1177 /* Write the last range */
Yinghai Lu12031a62008-05-02 02:40:22 -07001178 if (var_state.range_sizek != 0)
1179 range_to_mtrr_with_hole(&var_state, 0, 0);
Yinghai Lu12031a62008-05-02 02:40:22 -07001180
1181 num_reg = var_state.reg;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001182 /* Clear out the extra MTRR's */
Yinghai Luf5098d62008-04-29 20:25:58 -07001183 while (var_state.reg < num_var_ranges) {
Yinghai Lu12031a62008-05-02 02:40:22 -07001184 save_var_mtrr(var_state.reg, 0, 0, 0);
Yinghai Luf5098d62008-04-29 20:25:58 -07001185 var_state.reg++;
1186 }
Yinghai Lu12031a62008-05-02 02:40:22 -07001187
1188 return num_reg;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001189}
1190
Yinghai Lu12031a62008-05-02 02:40:22 -07001191struct mtrr_cleanup_result {
1192 unsigned long gran_sizek;
1193 unsigned long chunk_sizek;
1194 unsigned long lose_cover_sizek;
1195 unsigned int num_reg;
1196 int bad;
1197};
Yinghai Lu95ffa242008-04-29 03:52:33 -07001198
Yinghai Lu12031a62008-05-02 02:40:22 -07001199/*
Yinghai Lu46240652008-09-29 18:54:12 -07001200 * gran_size: 64K, 128K, 256K, 512K, 1M, 2M, ..., 2G
Yinghai Lu7fc23682008-09-27 00:30:07 -07001201 * chunk size: gran_size, ..., 2G
Yinghai Lu46240652008-09-29 18:54:12 -07001202 * so we need (1+16)*8
Yinghai Lu12031a62008-05-02 02:40:22 -07001203 */
Yinghai Lu46240652008-09-29 18:54:12 -07001204#define NUM_RESULT 136
Yinghai Lu12031a62008-05-02 02:40:22 -07001205#define PSHIFT (PAGE_SHIFT - 10)
Yinghai Lu95ffa242008-04-29 03:52:33 -07001206
Yinghai Lu12031a62008-05-02 02:40:22 -07001207static struct mtrr_cleanup_result __initdata result[NUM_RESULT];
Yinghai Lu12031a62008-05-02 02:40:22 -07001208static unsigned long __initdata min_loss_pfn[RANGE_NUM];
Yinghai Lu95ffa242008-04-29 03:52:33 -07001209
Yinghai Lu30604bb2008-10-14 18:59:18 -07001210static void __init print_out_mtrr_range_state(void)
Yinghai Lu95ffa242008-04-29 03:52:33 -07001211{
Yinghai Lu8bb39312008-10-02 23:26:59 -07001212 int i;
Yinghai Lu30604bb2008-10-14 18:59:18 -07001213 char start_factor = 'K', size_factor = 'K';
1214 unsigned long start_base, size_base;
1215 mtrr_type type;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001216
Yinghai Lu30604bb2008-10-14 18:59:18 -07001217 for (i = 0; i < num_var_ranges; i++) {
1218
1219 size_base = range_state[i].size_pfn << (PAGE_SHIFT - 10);
1220 if (!size_base)
1221 continue;
1222
1223 size_base = to_size_factor(size_base, &size_factor),
1224 start_base = range_state[i].base_pfn << (PAGE_SHIFT - 10);
1225 start_base = to_size_factor(start_base, &start_factor),
1226 type = range_state[i].type;
1227
1228 printk(KERN_DEBUG "reg %d, base: %ld%cB, range: %ld%cB, type %s\n",
1229 i, start_base, start_factor,
1230 size_base, size_factor,
1231 (type == MTRR_TYPE_UNCACHABLE) ? "UC" :
1232 ((type == MTRR_TYPE_WRPROT) ? "WP" :
1233 ((type == MTRR_TYPE_WRBACK) ? "WB" : "Other"))
1234 );
1235 }
1236}
1237
1238static int __init mtrr_need_cleanup(void)
1239{
1240 int i;
1241 mtrr_type type;
1242 unsigned long size;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001243 /* extra one for all 0 */
1244 int num[MTRR_NUM_TYPES + 1];
1245
Yinghai Lu95ffa242008-04-29 03:52:33 -07001246 /* check entries number */
1247 memset(num, 0, sizeof(num));
1248 for (i = 0; i < num_var_ranges; i++) {
Yinghai Lu12031a62008-05-02 02:40:22 -07001249 type = range_state[i].type;
1250 size = range_state[i].size_pfn;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001251 if (type >= MTRR_NUM_TYPES)
1252 continue;
1253 if (!size)
1254 type = MTRR_NUM_TYPES;
Yinghai Ludd552352008-10-04 14:50:33 -07001255 if (type == MTRR_TYPE_WRPROT)
1256 type = MTRR_TYPE_UNCACHABLE;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001257 num[type]++;
1258 }
1259
1260 /* check if we got UC entries */
1261 if (!num[MTRR_TYPE_UNCACHABLE])
1262 return 0;
1263
1264 /* check if we only had WB and UC */
1265 if (num[MTRR_TYPE_WRBACK] + num[MTRR_TYPE_UNCACHABLE] !=
1266 num_var_ranges - num[MTRR_NUM_TYPES])
1267 return 0;
1268
Yinghai Lu30604bb2008-10-14 18:59:18 -07001269 return 1;
1270}
1271
1272static unsigned long __initdata range_sums;
1273static void __init mtrr_calc_range_state(u64 chunk_size, u64 gran_size,
1274 unsigned long extra_remove_base,
1275 unsigned long extra_remove_size,
1276 int i)
1277{
1278 int num_reg;
1279 static struct res_range range_new[RANGE_NUM];
1280 static int nr_range_new;
1281 unsigned long range_sums_new;
1282
1283 /* convert ranges to var ranges state */
1284 num_reg = x86_setup_var_mtrrs(range, nr_range,
1285 chunk_size, gran_size);
1286
1287 /* we got new setting in range_state, check it */
1288 memset(range_new, 0, sizeof(range_new));
1289 nr_range_new = x86_get_mtrr_mem_range(range_new, 0,
1290 extra_remove_base, extra_remove_size);
1291 range_sums_new = sum_ranges(range_new, nr_range_new);
1292
1293 result[i].chunk_sizek = chunk_size >> 10;
1294 result[i].gran_sizek = gran_size >> 10;
1295 result[i].num_reg = num_reg;
1296 if (range_sums < range_sums_new) {
1297 result[i].lose_cover_sizek =
1298 (range_sums_new - range_sums) << PSHIFT;
1299 result[i].bad = 1;
1300 } else
1301 result[i].lose_cover_sizek =
1302 (range_sums - range_sums_new) << PSHIFT;
1303
1304 /* double check it */
1305 if (!result[i].bad && !result[i].lose_cover_sizek) {
1306 if (nr_range_new != nr_range ||
1307 memcmp(range, range_new, sizeof(range)))
1308 result[i].bad = 1;
1309 }
1310
1311 if (!result[i].bad && (range_sums - range_sums_new <
1312 min_loss_pfn[num_reg])) {
1313 min_loss_pfn[num_reg] =
1314 range_sums - range_sums_new;
1315 }
1316}
1317
1318static void __init mtrr_print_out_one_result(int i)
1319{
1320 char gran_factor, chunk_factor, lose_factor;
1321 unsigned long gran_base, chunk_base, lose_base;
1322
1323 gran_base = to_size_factor(result[i].gran_sizek, &gran_factor),
1324 chunk_base = to_size_factor(result[i].chunk_sizek, &chunk_factor),
1325 lose_base = to_size_factor(result[i].lose_cover_sizek, &lose_factor),
1326 printk(KERN_INFO "%sgran_size: %ld%c \tchunk_size: %ld%c \t",
1327 result[i].bad ? "*BAD*" : " ",
1328 gran_base, gran_factor, chunk_base, chunk_factor);
1329 printk(KERN_CONT "num_reg: %d \tlose cover RAM: %s%ld%c\n",
1330 result[i].num_reg, result[i].bad ? "-" : "",
1331 lose_base, lose_factor);
1332}
1333
1334static int __init mtrr_search_optimal_index(void)
1335{
1336 int i;
1337 int num_reg_good;
1338 int index_good;
1339
1340 if (nr_mtrr_spare_reg >= num_var_ranges)
1341 nr_mtrr_spare_reg = num_var_ranges - 1;
1342 num_reg_good = -1;
1343 for (i = num_var_ranges - nr_mtrr_spare_reg; i > 0; i--) {
1344 if (!min_loss_pfn[i])
1345 num_reg_good = i;
1346 }
1347
1348 index_good = -1;
1349 if (num_reg_good != -1) {
1350 for (i = 0; i < NUM_RESULT; i++) {
1351 if (!result[i].bad &&
1352 result[i].num_reg == num_reg_good &&
1353 !result[i].lose_cover_sizek) {
1354 index_good = i;
1355 break;
1356 }
1357 }
1358 }
1359
1360 return index_good;
1361}
1362
1363
1364static int __init mtrr_cleanup(unsigned address_bits)
1365{
1366 unsigned long extra_remove_base, extra_remove_size;
1367 unsigned long base, size, def, dummy;
1368 mtrr_type type;
1369 u64 chunk_size, gran_size;
1370 int index_good;
1371 int i;
1372
1373 if (!is_cpu(INTEL) || enable_mtrr_cleanup < 1)
1374 return 0;
1375 rdmsr(MTRRdefType_MSR, def, dummy);
1376 def &= 0xff;
1377 if (def != MTRR_TYPE_UNCACHABLE)
1378 return 0;
1379
1380 /* get it and store it aside */
1381 memset(range_state, 0, sizeof(range_state));
1382 for (i = 0; i < num_var_ranges; i++) {
1383 mtrr_if->get(i, &base, &size, &type);
1384 range_state[i].base_pfn = base;
1385 range_state[i].size_pfn = size;
1386 range_state[i].type = type;
1387 }
1388
1389 /* check if we need handle it and can handle it */
1390 if (!mtrr_need_cleanup())
1391 return 0;
1392
Yinghai Lu8bb39312008-10-02 23:26:59 -07001393 /* print original var MTRRs at first, for debugging: */
1394 printk(KERN_DEBUG "original variable MTRRs\n");
Yinghai Lu30604bb2008-10-14 18:59:18 -07001395 print_out_mtrr_range_state();
Yinghai Lu8bb39312008-10-02 23:26:59 -07001396
Yinghai Lu95ffa242008-04-29 03:52:33 -07001397 memset(range, 0, sizeof(range));
1398 extra_remove_size = 0;
Yinghai Lu279b0bb2008-09-18 23:55:27 -07001399 extra_remove_base = 1 << (32 - PAGE_SHIFT);
1400 if (mtrr_tom2)
Yinghai Lu12031a62008-05-02 02:40:22 -07001401 extra_remove_size =
1402 (mtrr_tom2 >> PAGE_SHIFT) - extra_remove_base;
Yinghai Lu12031a62008-05-02 02:40:22 -07001403 nr_range = x86_get_mtrr_mem_range(range, 0, extra_remove_base,
1404 extra_remove_size);
Yinghai Lu99e1aa12008-10-04 14:50:32 -07001405 /*
1406 * [0, 1M) should always be coverred by var mtrr with WB
1407 * and fixed mtrrs should take effective before var mtrr for it
1408 */
1409 nr_range = add_range_with_merge(range, nr_range, 0,
1410 (1ULL<<(20 - PAGE_SHIFT)) - 1);
1411 /* sort the ranges */
1412 sort(range, nr_range, sizeof(struct res_range), cmp_range, NULL);
1413
Yinghai Lu12031a62008-05-02 02:40:22 -07001414 range_sums = sum_ranges(range, nr_range);
1415 printk(KERN_INFO "total RAM coverred: %ldM\n",
1416 range_sums >> (20 - PAGE_SHIFT));
Yinghai Lu95ffa242008-04-29 03:52:33 -07001417
Yinghai Lu12031a62008-05-02 02:40:22 -07001418 if (mtrr_chunk_size && mtrr_gran_size) {
Yinghai Lu12031a62008-05-02 02:40:22 -07001419 i = 0;
Yinghai Lu30604bb2008-10-14 18:59:18 -07001420 mtrr_calc_range_state(mtrr_chunk_size, mtrr_gran_size,
1421 extra_remove_base, extra_remove_size, i);
Yinghai Lu12031a62008-05-02 02:40:22 -07001422
Yinghai Lu30604bb2008-10-14 18:59:18 -07001423 mtrr_print_out_one_result(i);
1424
Yinghai Lu12031a62008-05-02 02:40:22 -07001425 if (!result[i].bad) {
1426 set_var_mtrr_all(address_bits);
1427 return 1;
1428 }
1429 printk(KERN_INFO "invalid mtrr_gran_size or mtrr_chunk_size, "
1430 "will find optimal one\n");
Yinghai Lu12031a62008-05-02 02:40:22 -07001431 }
1432
1433 i = 0;
1434 memset(min_loss_pfn, 0xff, sizeof(min_loss_pfn));
1435 memset(result, 0, sizeof(result));
Yinghai Lu46240652008-09-29 18:54:12 -07001436 for (gran_size = (1ULL<<16); gran_size < (1ULL<<32); gran_size <<= 1) {
Yinghai Ludd7e5222008-09-29 18:54:11 -07001437
Yinghai Lu7fc23682008-09-27 00:30:07 -07001438 for (chunk_size = gran_size; chunk_size < (1ULL<<32);
Yinghai Lu12031a62008-05-02 02:40:22 -07001439 chunk_size <<= 1) {
Yinghai Lu12031a62008-05-02 02:40:22 -07001440
Yinghai Lu12031a62008-05-02 02:40:22 -07001441 if (i >= NUM_RESULT)
1442 continue;
1443
Yinghai Lu30604bb2008-10-14 18:59:18 -07001444 mtrr_calc_range_state(chunk_size, gran_size,
1445 extra_remove_base, extra_remove_size, i);
1446 if (debug_print) {
1447 mtrr_print_out_one_result(i);
1448 printk(KERN_INFO "\n");
Yinghai Lu12031a62008-05-02 02:40:22 -07001449 }
1450
Yinghai Lu12031a62008-05-02 02:40:22 -07001451 i++;
1452 }
1453 }
1454
Yinghai Lu12031a62008-05-02 02:40:22 -07001455 /* try to find the optimal index */
Yinghai Lu30604bb2008-10-14 18:59:18 -07001456 index_good = mtrr_search_optimal_index();
Yinghai Lu12031a62008-05-02 02:40:22 -07001457
1458 if (index_good != -1) {
1459 printk(KERN_INFO "Found optimal setting for mtrr clean up\n");
1460 i = index_good;
Yinghai Lu30604bb2008-10-14 18:59:18 -07001461 mtrr_print_out_one_result(i);
1462
Yinghai Lu12031a62008-05-02 02:40:22 -07001463 /* convert ranges to var ranges state */
1464 chunk_size = result[i].chunk_sizek;
1465 chunk_size <<= 10;
1466 gran_size = result[i].gran_sizek;
1467 gran_size <<= 10;
1468 x86_setup_var_mtrrs(range, nr_range, chunk_size, gran_size);
1469 set_var_mtrr_all(address_bits);
Yinghai Lu30604bb2008-10-14 18:59:18 -07001470 printk(KERN_DEBUG "New variable MTRRs\n");
1471 print_out_mtrr_range_state();
Yinghai Lu12031a62008-05-02 02:40:22 -07001472 return 1;
Yinghai Lu30604bb2008-10-14 18:59:18 -07001473 } else {
1474 /* print out all */
1475 for (i = 0; i < NUM_RESULT; i++)
1476 mtrr_print_out_one_result(i);
Yinghai Lu12031a62008-05-02 02:40:22 -07001477 }
1478
1479 printk(KERN_INFO "mtrr_cleanup: can not find optimal value\n");
1480 printk(KERN_INFO "please specify mtrr_gran_size/mtrr_chunk_size\n");
1481
1482 return 0;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001483}
Yinghai Lu12031a62008-05-02 02:40:22 -07001484#else
1485static int __init mtrr_cleanup(unsigned address_bits)
1486{
1487 return 0;
1488}
1489#endif
1490
1491static int __initdata changed_by_mtrr_cleanup;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001492
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001493static int disable_mtrr_trim;
1494
1495static int __init disable_mtrr_trim_setup(char *str)
1496{
1497 disable_mtrr_trim = 1;
1498 return 0;
1499}
1500early_param("disable_mtrr_trim", disable_mtrr_trim_setup);
1501
1502/*
1503 * Newer AMD K8s and later CPUs have a special magic MSR way to force WB
1504 * for memory >4GB. Check for that here.
1505 * Note this won't check if the MTRRs < 4GB where the magic bit doesn't
1506 * apply to are wrong, but so far we don't know of any such case in the wild.
1507 */
1508#define Tom2Enabled (1U << 21)
1509#define Tom2ForceMemTypeWB (1U << 22)
1510
Yinghai Lu35605a12008-03-24 16:02:01 -07001511int __init amd_special_default_mtrr(void)
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001512{
1513 u32 l, h;
1514
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001515 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
1516 return 0;
1517 if (boot_cpu_data.x86 < 0xf || boot_cpu_data.x86 > 0x11)
1518 return 0;
1519 /* In case some hypervisor doesn't pass SYSCFG through */
1520 if (rdmsr_safe(MSR_K8_SYSCFG, &l, &h) < 0)
1521 return 0;
1522 /*
1523 * Memory between 4GB and top of mem is forced WB by this magic bit.
1524 * Reserved before K8RevF, but should be zero there.
1525 */
1526 if ((l & (Tom2Enabled | Tom2ForceMemTypeWB)) ==
1527 (Tom2Enabled | Tom2ForceMemTypeWB))
1528 return 1;
1529 return 0;
1530}
1531
Yinghai Lu12031a62008-05-02 02:40:22 -07001532static u64 __init real_trim_memory(unsigned long start_pfn,
1533 unsigned long limit_pfn)
Yinghai Lu42651f12008-04-29 01:59:49 -07001534{
1535 u64 trim_start, trim_size;
Yinghai Lu8a374022008-04-29 20:25:16 -07001536 trim_start = start_pfn;
Yinghai Lu42651f12008-04-29 01:59:49 -07001537 trim_start <<= PAGE_SHIFT;
1538 trim_size = limit_pfn;
1539 trim_size <<= PAGE_SHIFT;
1540 trim_size -= trim_start;
Yinghai Lu8a374022008-04-29 20:25:16 -07001541
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001542 return e820_update_range(trim_start, trim_size, E820_RAM,
Yinghai Lu42651f12008-04-29 01:59:49 -07001543 E820_RESERVED);
1544}
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001545/**
1546 * mtrr_trim_uncached_memory - trim RAM not covered by MTRRs
Randy Dunlapf5106d92008-02-18 13:10:44 -08001547 * @end_pfn: ending page frame number
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001548 *
1549 * Some buggy BIOSes don't setup the MTRRs properly for systems with certain
1550 * memory configurations. This routine checks that the highest MTRR matches
1551 * the end of memory, to make sure the MTRRs having a write back type cover
1552 * all of the memory the kernel is intending to use. If not, it'll trim any
1553 * memory off the end by adjusting end_pfn, removing it from the kernel's
1554 * allocation pools, warning the user with an obnoxious message.
1555 */
1556int __init mtrr_trim_uncached_memory(unsigned long end_pfn)
1557{
Yinghai Lu20651af2008-02-06 22:39:45 +01001558 unsigned long i, base, size, highest_pfn = 0, def, dummy;
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001559 mtrr_type type;
Yinghai Lu12031a62008-05-02 02:40:22 -07001560 u64 total_trim_size;
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001561
Yinghai Lu42651f12008-04-29 01:59:49 -07001562 /* extra one for all 0 */
1563 int num[MTRR_NUM_TYPES + 1];
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001564 /*
1565 * Make sure we only trim uncachable memory on machines that
1566 * support the Intel MTRR architecture:
1567 */
Yinghai Lu093af8d2008-01-30 13:33:32 +01001568 if (!is_cpu(INTEL) || disable_mtrr_trim)
1569 return 0;
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001570 rdmsr(MTRRdefType_MSR, def, dummy);
1571 def &= 0xff;
Yinghai Lu093af8d2008-01-30 13:33:32 +01001572 if (def != MTRR_TYPE_UNCACHABLE)
1573 return 0;
1574
Yinghai Lu12031a62008-05-02 02:40:22 -07001575 /* get it and store it aside */
1576 memset(range_state, 0, sizeof(range_state));
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001577 for (i = 0; i < num_var_ranges; i++) {
1578 mtrr_if->get(i, &base, &size, &type);
Yinghai Lu12031a62008-05-02 02:40:22 -07001579 range_state[i].base_pfn = base;
1580 range_state[i].size_pfn = size;
1581 range_state[i].type = type;
1582 }
1583
1584 /* Find highest cached pfn */
1585 for (i = 0; i < num_var_ranges; i++) {
1586 type = range_state[i].type;
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001587 if (type != MTRR_TYPE_WRBACK)
1588 continue;
Yinghai Lu12031a62008-05-02 02:40:22 -07001589 base = range_state[i].base_pfn;
1590 size = range_state[i].size_pfn;
Yinghai Lu20651af2008-02-06 22:39:45 +01001591 if (highest_pfn < base + size)
1592 highest_pfn = base + size;
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001593 }
1594
Yinghai Lu093af8d2008-01-30 13:33:32 +01001595 /* kvm/qemu doesn't have mtrr set right, don't trim them all */
Yinghai Lu20651af2008-02-06 22:39:45 +01001596 if (!highest_pfn) {
Arjan van de Venbde78a72008-07-08 09:51:56 -07001597 WARN(!kvm_para_available(), KERN_WARNING
Joerg Roedel4147c872008-02-21 15:50:14 +01001598 "WARNING: strange, CPU MTRRs all blank?\n");
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001599 return 0;
Yinghai Lu093af8d2008-01-30 13:33:32 +01001600 }
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001601
Yinghai Lu42651f12008-04-29 01:59:49 -07001602 /* check entries number */
1603 memset(num, 0, sizeof(num));
1604 for (i = 0; i < num_var_ranges; i++) {
Yinghai Lu12031a62008-05-02 02:40:22 -07001605 type = range_state[i].type;
Yinghai Lu42651f12008-04-29 01:59:49 -07001606 if (type >= MTRR_NUM_TYPES)
1607 continue;
Yinghai Lu12031a62008-05-02 02:40:22 -07001608 size = range_state[i].size_pfn;
Yinghai Lu42651f12008-04-29 01:59:49 -07001609 if (!size)
1610 type = MTRR_NUM_TYPES;
1611 num[type]++;
1612 }
1613
1614 /* no entry for WB? */
1615 if (!num[MTRR_TYPE_WRBACK])
1616 return 0;
1617
1618 /* check if we only had WB and UC */
1619 if (num[MTRR_TYPE_WRBACK] + num[MTRR_TYPE_UNCACHABLE] !=
1620 num_var_ranges - num[MTRR_NUM_TYPES])
1621 return 0;
1622
1623 memset(range, 0, sizeof(range));
1624 nr_range = 0;
1625 if (mtrr_tom2) {
1626 range[nr_range].start = (1ULL<<(32 - PAGE_SHIFT));
1627 range[nr_range].end = (mtrr_tom2 >> PAGE_SHIFT) - 1;
1628 if (highest_pfn < range[nr_range].end + 1)
1629 highest_pfn = range[nr_range].end + 1;
1630 nr_range++;
1631 }
1632 nr_range = x86_get_mtrr_mem_range(range, nr_range, 0, 0);
1633
Yinghai Lu12031a62008-05-02 02:40:22 -07001634 total_trim_size = 0;
Yinghai Lu8a374022008-04-29 20:25:16 -07001635 /* check the head */
Yinghai Lu42651f12008-04-29 01:59:49 -07001636 if (range[0].start)
Yinghai Lu12031a62008-05-02 02:40:22 -07001637 total_trim_size += real_trim_memory(0, range[0].start);
Yinghai Lu8a374022008-04-29 20:25:16 -07001638 /* check the holes */
1639 for (i = 0; i < nr_range - 1; i++) {
Yinghai Lu42651f12008-04-29 01:59:49 -07001640 if (range[i].end + 1 < range[i+1].start)
Yinghai Lu12031a62008-05-02 02:40:22 -07001641 total_trim_size += real_trim_memory(range[i].end + 1,
1642 range[i+1].start);
Yinghai Lu42651f12008-04-29 01:59:49 -07001643 }
Yinghai Lu8a374022008-04-29 20:25:16 -07001644 /* check the top */
1645 i = nr_range - 1;
1646 if (range[i].end + 1 < end_pfn)
Yinghai Lu12031a62008-05-02 02:40:22 -07001647 total_trim_size += real_trim_memory(range[i].end + 1,
1648 end_pfn);
Yinghai Lu42651f12008-04-29 01:59:49 -07001649
Yinghai Lu12031a62008-05-02 02:40:22 -07001650 if (total_trim_size) {
Yinghai Lu42651f12008-04-29 01:59:49 -07001651 printk(KERN_WARNING "WARNING: BIOS bug: CPU MTRRs don't cover"
1652 " all of memory, losing %lluMB of RAM.\n",
Yinghai Lu12031a62008-05-02 02:40:22 -07001653 total_trim_size >> 20);
Yinghai Lu42651f12008-04-29 01:59:49 -07001654
Yinghai Lu12031a62008-05-02 02:40:22 -07001655 if (!changed_by_mtrr_cleanup)
Yinghai Lu8a374022008-04-29 20:25:16 -07001656 WARN_ON(1);
Yinghai Lu42651f12008-04-29 01:59:49 -07001657
Yinghai Lu8a374022008-04-29 20:25:16 -07001658 printk(KERN_INFO "update e820 for mtrr\n");
Yinghai Lu42651f12008-04-29 01:59:49 -07001659 update_e820();
Yinghai Lu8a374022008-04-29 20:25:16 -07001660
1661 return 1;
Yinghai Lu42651f12008-04-29 01:59:49 -07001662 }
1663
Yinghai Lu8a374022008-04-29 20:25:16 -07001664 return 0;
Jesse Barnes99fc8d42008-01-30 13:33:18 +01001665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667/**
Shaohua Li3b520b22005-07-07 17:56:38 -07001668 * mtrr_bp_init - initialize mtrrs on the boot CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 *
1670 * This needs to be called early; before any of the other CPUs are
1671 * initialized (i.e. before smp_init()).
1672 *
1673 */
Sam Ravnborg9ef231a2007-07-21 17:10:39 +02001674void __init mtrr_bp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Yinghai Lu95ffa242008-04-29 03:52:33 -07001676 u32 phys_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 init_ifs();
1678
Yinghai Lu95ffa242008-04-29 03:52:33 -07001679 phys_addr = 32;
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (cpu_has_mtrr) {
1682 mtrr_if = &generic_mtrr_ops;
1683 size_or_mask = 0xff000000; /* 36 bits */
1684 size_and_mask = 0x00f00000;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001685 phys_addr = 36;
Andi Kleen1f2c9582005-04-16 15:25:10 -07001686
1687 /* This is an AMD specific MSR, but we assume(hope?) that
1688 Intel will implement it to when they extend the address
1689 bus of the Xeon. */
1690 if (cpuid_eax(0x80000000) >= 0x80000008) {
Andi Kleen1f2c9582005-04-16 15:25:10 -07001691 phys_addr = cpuid_eax(0x80000008) & 0xff;
Shaohua Liaf9c1422005-11-05 17:25:54 +01001692 /* CPUID workaround for Intel 0F33/0F34 CPU */
1693 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
1694 boot_cpu_data.x86 == 0xF &&
1695 boot_cpu_data.x86_model == 0x3 &&
1696 (boot_cpu_data.x86_mask == 0x3 ||
1697 boot_cpu_data.x86_mask == 0x4))
1698 phys_addr = 36;
1699
Andreas Herrmann6c5806c2007-02-13 13:26:23 +01001700 size_or_mask = ~((1ULL << (phys_addr - PAGE_SHIFT)) - 1);
1701 size_and_mask = ~size_or_mask & 0xfffff00000ULL;
Andi Kleen1f2c9582005-04-16 15:25:10 -07001702 } else if (boot_cpu_data.x86_vendor == X86_VENDOR_CENTAUR &&
1703 boot_cpu_data.x86 == 6) {
1704 /* VIA C* family have Intel style MTRRs, but
1705 don't support PAE */
1706 size_or_mask = 0xfff00000; /* 32 bits */
1707 size_and_mask = 0;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001708 phys_addr = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 } else {
1711 switch (boot_cpu_data.x86_vendor) {
1712 case X86_VENDOR_AMD:
1713 if (cpu_has_k6_mtrr) {
1714 /* Pre-Athlon (K6) AMD CPU MTRRs */
1715 mtrr_if = mtrr_ops[X86_VENDOR_AMD];
1716 size_or_mask = 0xfff00000; /* 32 bits */
1717 size_and_mask = 0;
1718 }
1719 break;
1720 case X86_VENDOR_CENTAUR:
1721 if (cpu_has_centaur_mcr) {
1722 mtrr_if = mtrr_ops[X86_VENDOR_CENTAUR];
1723 size_or_mask = 0xfff00000; /* 32 bits */
1724 size_and_mask = 0;
1725 }
1726 break;
1727 case X86_VENDOR_CYRIX:
1728 if (cpu_has_cyrix_arr) {
1729 mtrr_if = mtrr_ops[X86_VENDOR_CYRIX];
1730 size_or_mask = 0xfff00000; /* 32 bits */
1731 size_and_mask = 0;
1732 }
1733 break;
1734 default:
1735 break;
1736 }
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (mtrr_if) {
1740 set_num_var_ranges();
1741 init_table();
Yinghai Lu95ffa242008-04-29 03:52:33 -07001742 if (use_intel()) {
Shaohua Li3b520b22005-07-07 17:56:38 -07001743 get_mtrr_state();
Yinghai Lu95ffa242008-04-29 03:52:33 -07001744
Yinghai Lu12031a62008-05-02 02:40:22 -07001745 if (mtrr_cleanup(phys_addr)) {
1746 changed_by_mtrr_cleanup = 1;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001747 mtrr_if->set_all();
Yinghai Lu12031a62008-05-02 02:40:22 -07001748 }
Yinghai Lu95ffa242008-04-29 03:52:33 -07001749
1750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Shaohua Li3b520b22005-07-07 17:56:38 -07001754void mtrr_ap_init(void)
1755{
1756 unsigned long flags;
1757
1758 if (!mtrr_if || !use_intel())
1759 return;
1760 /*
Ingo Molnar14cc3e22006-03-26 01:37:14 -08001761 * Ideally we should hold mtrr_mutex here to avoid mtrr entries changed,
Shaohua Li3b520b22005-07-07 17:56:38 -07001762 * but this routine will be called in cpu boot time, holding the lock
1763 * breaks it. This routine is called in two cases: 1.very earily time
1764 * of software resume, when there absolutely isn't mtrr entry changes;
1765 * 2.cpu hotadd time. We let mtrr_add/del_page hold cpuhotplug lock to
1766 * prevent mtrr entry changes
1767 */
1768 local_irq_save(flags);
1769
1770 mtrr_if->set_all();
1771
1772 local_irq_restore(flags);
1773}
1774
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +02001775/**
1776 * Save current fixed-range MTRR state of the BSP
1777 */
1778void mtrr_save_state(void)
1779{
Jens Axboe8691e5a2008-06-06 11:18:06 +02001780 smp_call_function_single(0, mtrr_save_fixed_ranges, NULL, 1);
Bernhard Kaindl2b1f6272007-05-02 19:27:17 +02001781}
1782
Shaohua Li3b520b22005-07-07 17:56:38 -07001783static int __init mtrr_init_finialize(void)
1784{
1785 if (!mtrr_if)
1786 return 0;
Yinghai Lu95ffa242008-04-29 03:52:33 -07001787 if (use_intel()) {
Yinghai Lu12031a62008-05-02 02:40:22 -07001788 if (!changed_by_mtrr_cleanup)
Yinghai Lu95ffa242008-04-29 03:52:33 -07001789 mtrr_state_warn();
1790 } else {
Simon Arlott27b46d72007-10-20 01:13:56 +02001791 /* The CPUs haven't MTRR and seem to not support SMP. They have
Shaohua Li3b520b22005-07-07 17:56:38 -07001792 * specific drivers, we use a tricky method to support
1793 * suspend/resume for them.
1794 * TBD: is there any system with such CPU which supports
1795 * suspend/resume? if no, we should remove the code.
1796 */
1797 sysdev_driver_register(&cpu_sysdev_class,
1798 &mtrr_sysdev_driver);
1799 }
1800 return 0;
1801}
1802subsys_initcall(mtrr_init_finialize);