import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / arch / x86_64 / kernel / mtrr.c
1 /*  x86-64 MTRR (Memory Type Range Register) driver.
2         Based largely upon arch/i386/kernel/mtrr.c
3
4         Copyright (C) 1997-2000  Richard Gooch
5         Copyright (C) 2002 Dave Jones.
6
7         This library is free software; you can redistribute it and/or
8         modify it under the terms of the GNU Library General Public
9         License as published by the Free Software Foundation; either
10         version 2 of the License, or (at your option) any later version.
11
12         This library is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15         Library General Public License for more details.
16
17         You should have received a copy of the GNU Library General Public
18         License along with this library; if not, write to the Free
19         Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21         (For earlier history, see arch/i386/kernel/mtrr.c)
22         v2.00   September 2001  Dave Jones <davej@suse.de>
23           Initial rewrite for x86-64.
24           Removal of non-Intel style MTRR code.
25         v2.01  June 2002  Dave Jones <davej@suse.de>
26           Removal of redundant abstraction layer.
27           64-bit fixes.
28         v2.02  July 2002  Dave Jones <davej@suse.de>
29           Fix gentry inconsistencies between kernel/userspace.
30           More casts to clean up warnings.
31 */
32
33 #include <linux/types.h>
34 #include <linux/errno.h>
35 #include <linux/sched.h>
36 #include <linux/tty.h>
37 #include <linux/timer.h>
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/wait.h>
41 #include <linux/string.h>
42 #include <linux/slab.h>
43 #include <linux/ioport.h>
44 #include <linux/delay.h>
45 #include <linux/fs.h>
46 #include <linux/ctype.h>
47 #include <linux/proc_fs.h>
48 #include <linux/devfs_fs_kernel.h>
49 #include <linux/mm.h>
50 #include <linux/module.h>
51 #define MTRR_NEED_STRINGS
52 #include <asm/mtrr.h>
53 #include <linux/init.h>
54 #include <linux/smp.h>
55 #include <linux/smp_lock.h>
56 #include <linux/agp_backend.h>
57
58 #include <asm/uaccess.h>
59 #include <asm/io.h>
60 #include <asm/processor.h>
61 #include <asm/system.h>
62 #include <asm/pgtable.h>
63 #include <asm/segment.h>
64 #include <asm/bitops.h>
65 #include <asm/atomic.h>
66 #include <asm/msr.h>
67
68 #include <asm/hardirq.h>
69 #include <linux/irq.h>
70
71 #define MTRR_VERSION "2.02 (20020716)"
72
73 #define MTRR_BEG_BIT 12
74 #define MTRR_END_BIT 7
75
76 #undef Dprintk
77
78 #define Dprintk(...) 
79
80 #define TRUE  1
81 #define FALSE 0
82
83 #define MSR_MTRRphysBase(reg) (0x200 + 2 * (reg))
84 #define MSR_MTRRphysMask(reg) (0x200 + 2 * (reg) + 1)
85
86 #define NUM_FIXED_RANGES 88
87
88 #define MTRR_CHANGE_MASK_FIXED 0x01
89 #define MTRR_CHANGE_MASK_VARIABLE 0x02
90 #define MTRR_CHANGE_MASK_DEFTYPE 0x04
91
92 typedef u8 mtrr_type;
93
94 #define LINE_SIZE 80
95
96 #ifdef CONFIG_SMP
97 #define set_mtrr(reg,base,size,type) set_mtrr_smp (reg, base, size, type)
98 #else
99 #define set_mtrr(reg,base,size,type) set_mtrr_up (reg, base, size, type, TRUE)
100 #endif
101
102 #if defined(CONFIG_PROC_FS) || defined(CONFIG_DEVFS_FS)
103 #define USERSPACE_INTERFACE
104 #endif
105
106 #ifdef USERSPACE_INTERFACE
107 static char *ascii_buffer;
108 static unsigned int ascii_buf_bytes;
109 static void compute_ascii (void);
110 #else
111 #define compute_ascii() while (0)
112 #endif
113
114 static unsigned int *usage_table;
115 static DECLARE_MUTEX (mtrr_lock);
116
117 struct set_mtrr_context {
118         u32 deftype_lo;
119         u32 deftype_hi;
120         unsigned long flags;
121         u64 cr4val;
122 };
123
124
125 /*  Put the processor into a state where MTRRs can be safely set  */
126 static void set_mtrr_prepare (struct set_mtrr_context *ctxt)
127 {
128         u64 cr0;
129
130         /* Disable interrupts locally */
131         __save_flags(ctxt->flags);
132         __cli();
133
134         /* Save value of CR4 and clear Page Global Enable (bit 7) */
135         if (test_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability)) {
136                 ctxt->cr4val = read_cr4();
137                 write_cr4(ctxt->cr4val & ~(1UL << 7));
138         }
139
140         /* Disable and flush caches. Note that wbinvd flushes the TLBs as
141            a side-effect */
142         cr0 = read_cr0() | 0x40000000;
143         wbinvd();
144         write_cr0(cr0);
145         wbinvd();
146
147         rdmsr(MSR_MTRRdefType, ctxt->deftype_lo, ctxt->deftype_hi);
148 }
149
150 static void set_mtrr_disable(struct set_mtrr_context *ctxt) 
151
152         /* Disable MTRRs, and set the default type to uncached */
153         wrmsr(MSR_MTRRdefType, ctxt->deftype_lo & 0xf300UL, ctxt->deftype_hi);
154
155
156 /* Restore the processor after a set_mtrr_prepare */
157 static void set_mtrr_done (struct set_mtrr_context *ctxt)
158 {
159         /* Flush caches and TLBs */
160         wbinvd();
161
162         /* Restore MTRRdefType */
163         wrmsr(MSR_MTRRdefType, ctxt->deftype_lo, ctxt->deftype_hi);
164
165         /* Enable caches */
166         write_cr0(read_cr0() & 0xbfffffff);
167
168         /* Restore value of CR4 */
169         if (test_bit(X86_FEATURE_PGE, boot_cpu_data.x86_capability))
170                 write_cr4 (ctxt->cr4val);
171
172         /* Re-enable interrupts locally (if enabled previously) */
173         __restore_flags(ctxt->flags);
174 }
175
176
177 /*  This function returns the number of variable MTRRs  */
178 static unsigned int get_num_var_ranges (void)
179 {
180         u32 config, dummy;
181
182         rdmsr (MSR_MTRRcap, config, dummy);
183         return (config & 0xff);
184 }
185
186
187 /*  Returns non-zero if we have the write-combining memory type  */
188 static int have_wrcomb (void)
189 {
190         u32 config, dummy;
191
192         rdmsr (MSR_MTRRcap, config, dummy);
193         return (config & (1 << 10));
194 }
195
196
197 static u64 size_or_mask, size_and_mask;
198
199 static void get_mtrr (unsigned int reg, u64 *base, u32 *size, mtrr_type * type)
200 {
201         u32 mask_lo, mask_hi;
202         u32 base_lo, base_hi;
203
204         rdmsr (MSR_MTRRphysMask(reg), mask_lo, mask_hi);
205         if ((mask_lo & 0x800) == 0) {
206                 /*  Invalid (i.e. free) range  */
207                 *base = 0;
208                 *size = 0;
209                 *type = 0;
210                 return;
211         }
212
213         rdmsr (MSR_MTRRphysBase(reg), base_lo, base_hi);
214
215         /* Work out the shifted address mask */
216         mask_lo = size_or_mask | mask_hi << (32 - PAGE_SHIFT) | 
217                   mask_lo >> PAGE_SHIFT; 
218
219         /* This works correctly if size is a power of two, i.e. a
220            continguous range. */
221         *size = -mask_lo;
222         *base = base_hi << (32 - PAGE_SHIFT) | base_lo >> PAGE_SHIFT; 
223         *type = base_lo & 0xff; 
224 }
225
226 /*
227  * Set variable MTRR register on the local CPU.
228  *  <reg> The register to set.
229  *  <base> The base address of the region.
230  *  <size> The size of the region. If this is 0 the region is disabled.
231  *  <type> The type of the region.
232  *  <do_safe> If TRUE, do the change safely. If FALSE, safety measures should
233  *  be done externally.
234  */
235 static void set_mtrr_up (unsigned int reg, u64 base,
236                    u32 size, mtrr_type type, int do_safe)
237 {
238         struct set_mtrr_context ctxt;
239
240         if (do_safe) { 
241                 set_mtrr_prepare (&ctxt);
242                 set_mtrr_disable (&ctxt); 
243         } 
244
245         if (size == 0) {
246                 /* The invalid bit is kept in the mask, so we simply clear the
247                    relevant mask register to disable a range. */
248                 wrmsr (MSR_MTRRphysMask(reg), 0, 0);
249         } else {
250                 wrmsr (MSR_MTRRphysBase(reg), base << PAGE_SHIFT | type, 
251                         (base & size_and_mask) >> (32 - PAGE_SHIFT));
252                 wrmsr(MSR_MTRRphysMask(reg), -size << PAGE_SHIFT | 0x800,
253                         (-size & size_and_mask) >> (32 - PAGE_SHIFT));
254         }
255         if (do_safe)
256                 set_mtrr_done (&ctxt);
257 }
258
259
260 #ifdef CONFIG_SMP
261
262 struct mtrr_var_range {
263         u32 base_lo;
264         u32 base_hi;
265         u32 mask_lo;
266         u32 mask_hi;
267 };
268
269 /*  Get the MSR pair relating to a var range  */
270 static void __init get_mtrr_var_range (unsigned int index,
271                 struct mtrr_var_range *vr)
272 {
273         rdmsr (MSR_MTRRphysBase(index), vr->base_lo, vr->base_hi);
274         rdmsr (MSR_MTRRphysMask(index), vr->mask_lo, vr->mask_hi);
275 }
276
277
278 /*  Set the MSR pair relating to a var range. Returns TRUE if
279     changes are made  */
280 static int __init set_mtrr_var_range_testing (unsigned int index,
281                 struct mtrr_var_range *vr)
282 {
283         u32 lo, hi;
284         int changed = FALSE;
285
286         rdmsr (MSR_MTRRphysBase(index), lo, hi);
287         if ((vr->base_lo & 0xfffff0ff) != (lo & 0xfffff0ff) ||
288                 (vr->base_hi & 0x000fffff) != (hi & 0x000fffff)) {
289                 wrmsr (MSR_MTRRphysBase(index), vr->base_lo, vr->base_hi);
290                 changed = TRUE;
291         }
292
293         rdmsr (MSR_MTRRphysMask(index), lo, hi);
294         if ((vr->mask_lo & 0xfffff800) != (lo & 0xfffff800) ||
295                 (vr->mask_hi & 0x000fffff) != (hi & 0x000fffff)) {
296                 wrmsr (MSR_MTRRphysMask(index), vr->mask_lo, vr->mask_hi);
297                 changed = TRUE;
298         }
299         return changed;
300 }
301
302
303 static void __init get_fixed_ranges (mtrr_type * frs)
304 {
305         u32 *p = (u32 *) frs;
306         int i;
307
308         rdmsr (MSR_MTRRfix64K_00000, p[0], p[1]);
309
310         for (i = 0; i < 2; i++)
311                 rdmsr (MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
312         for (i = 0; i < 8; i++)
313                 rdmsr (MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
314 }
315
316
317 static int __init set_fixed_ranges_testing (mtrr_type * frs)
318 {
319         u32 *p = (u32 *) frs;
320         int changed = FALSE;
321         int i;
322         u32 lo, hi;
323
324         Dprintk (KERN_INFO "mtrr: rdmsr 64K_00000\n");
325         rdmsr (MSR_MTRRfix64K_00000, lo, hi);
326         if (p[0] != lo || p[1] != hi) {
327                 Dprintk (KERN_INFO "mtrr: Writing %x:%x to 64K MSR. lohi were %x:%x\n", p[0], p[1], lo, hi);
328                 wrmsr (MSR_MTRRfix64K_00000, p[0], p[1]);
329                 changed = TRUE;
330         }
331
332         Dprintk (KERN_INFO "mtrr: rdmsr 16K_80000\n");
333         for (i = 0; i < 2; i++) {
334                 rdmsr (MSR_MTRRfix16K_80000 + i, lo, hi);
335                 if (p[2 + i * 2] != lo || p[3 + i * 2] != hi) {
336                         Dprintk (KERN_INFO "mtrr: Writing %x:%x to 16K MSR%d. lohi were %x:%x\n", p[2 + i * 2], p[3 + i * 2], i, lo, hi );
337                         wrmsr (MSR_MTRRfix16K_80000 + i, p[2 + i * 2], p[3 + i * 2]);
338                         changed = TRUE;
339                 }
340         }
341
342         Dprintk (KERN_INFO "mtrr: rdmsr 4K_C0000\n");
343         for (i = 0; i < 8; i++) {
344                 rdmsr (MSR_MTRRfix4K_C0000 + i, lo, hi);
345                 Dprintk (KERN_INFO "mtrr: MTRRfix4K_C0000+%d = %x:%x\n", i, lo, hi);
346                 if (p[6 + i * 2] != lo || p[7 + i * 2] != hi) {
347                         Dprintk (KERN_INFO "mtrr: Writing %x:%x to 4K MSR%d. lohi were %x:%x\n", p[6 + i * 2], p[7 + i * 2], i, lo, hi);
348                         wrmsr (MSR_MTRRfix4K_C0000 + i, p[6 + i * 2], p[7 + i * 2]);
349                         changed = TRUE;
350                 }
351         }
352         return changed;
353 }
354
355
356 struct mtrr_state {
357         unsigned int num_var_ranges;
358         struct mtrr_var_range *var_ranges;
359         mtrr_type fixed_ranges[NUM_FIXED_RANGES];
360         mtrr_type def_type;
361         unsigned char enabled;
362 };
363
364
365 /*  Grab all of the MTRR state for this CPU into *state  */
366 static void __init get_mtrr_state (struct mtrr_state *state)
367 {
368         unsigned int nvrs, i;
369         struct mtrr_var_range *vrs;
370         u32 lo, dummy;
371
372         nvrs = state->num_var_ranges = get_num_var_ranges();
373         vrs = state->var_ranges
374             = kmalloc (nvrs * sizeof (struct mtrr_var_range), GFP_KERNEL);
375         if (vrs == NULL)
376                 nvrs = state->num_var_ranges = 0;
377
378         for (i = 0; i < nvrs; i++)
379                 get_mtrr_var_range (i, &vrs[i]);
380         get_fixed_ranges (state->fixed_ranges);
381
382         rdmsr (MSR_MTRRdefType, lo, dummy);
383         state->def_type = (lo & 0xff);
384         state->enabled = (lo & 0xc00) >> 10;
385 }
386
387
388 /*  Free resources associated with a struct mtrr_state  */
389 static void __init finalize_mtrr_state (struct mtrr_state *state)
390 {
391         if (state->var_ranges)
392                 kfree (state->var_ranges);
393 }
394
395
396 /*
397  * Set the MTRR state for this CPU.
398  *  <state> The MTRR state information to read.
399  *  <ctxt> Some relevant CPU context.
400  *  [NOTE] The CPU must already be in a safe state for MTRR changes.
401  *  [RETURNS] 0 if no changes made, else a mask indication what was changed.
402  */
403 static u64 __init set_mtrr_state (struct mtrr_state *state,
404                 struct set_mtrr_context *ctxt)
405 {
406         unsigned int i;
407         u64 change_mask = 0;
408
409         for (i = 0; i < state->num_var_ranges; i++)
410                 if (set_mtrr_var_range_testing (i, &state->var_ranges[i]))
411                         change_mask |= MTRR_CHANGE_MASK_VARIABLE;
412
413         if (set_fixed_ranges_testing (state->fixed_ranges))
414                 change_mask |= MTRR_CHANGE_MASK_FIXED;
415         /* Set_mtrr_restore restores the old value of MTRRdefType,
416            so to set it we fiddle with the saved value  */
417         if ((ctxt->deftype_lo & 0xff) != state->def_type
418             || ((ctxt->deftype_lo & 0xc00) >> 10) != state->enabled) {
419                 ctxt->deftype_lo |= (state->def_type | state->enabled << 10);
420                 change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
421         }
422
423         return change_mask;
424 }
425
426
427 static atomic_t undone_count;
428 static volatile int wait_barrier_mtrr_disable = FALSE;
429 static volatile int wait_barrier_execute = FALSE;
430 static volatile int wait_barrier_cache_enable = FALSE;
431
432 struct set_mtrr_data {
433         u64 smp_base;
434         u32 smp_size;
435         unsigned int smp_reg;
436         mtrr_type smp_type;
437 };
438
439 /*
440  * Synchronisation handler. Executed by "other" CPUs.
441  */
442 static void ipi_handler (void *info)
443 {
444         struct set_mtrr_data *data = info;
445         struct set_mtrr_context ctxt;
446
447         set_mtrr_prepare (&ctxt);
448         /* Notify master that I've flushed and disabled my cache  */
449         atomic_dec (&undone_count);
450
451         while (wait_barrier_mtrr_disable) {
452                 rep_nop();
453                 barrier ();
454         }       
455         set_mtrr_disable (&ctxt); 
456         /* wait again for disable confirmation*/
457         atomic_dec (&undone_count);
458         while (wait_barrier_execute) { 
459                 rep_nop(); 
460                 barrier(); 
461         }       
462
463         /* The master has cleared me to execute  */
464         set_mtrr_up (data->smp_reg, data->smp_base, data->smp_size,
465                         data->smp_type, FALSE);
466
467         /* Notify master CPU that I've executed the function  */
468         atomic_dec (&undone_count);
469
470         /* Wait for master to clear me to enable cache and return  */
471         while (wait_barrier_cache_enable) {
472                 rep_nop();
473                 barrier ();
474         }       
475         set_mtrr_done (&ctxt);
476 }
477
478
479 static void set_mtrr_smp (unsigned int reg, u64 base, u32 size, mtrr_type type)
480 {
481         struct set_mtrr_data data;
482         struct set_mtrr_context ctxt;
483
484         data.smp_reg = reg;
485         data.smp_base = base;
486         data.smp_size = size;
487         data.smp_type = type;
488         wait_barrier_execute = TRUE;
489         wait_barrier_cache_enable = TRUE;
490         wait_barrier_mtrr_disable = TRUE;
491         atomic_set (&undone_count, smp_num_cpus - 1);
492
493         /*  Start the ball rolling on other CPUs  */
494         if (smp_call_function (ipi_handler, &data, 1, 0) != 0)
495                 panic ("mtrr: timed out waiting for other CPUs\n");
496
497         /* Flush and disable the local CPU's cache */
498         set_mtrr_prepare (&ctxt);
499         while (atomic_read (&undone_count) > 0) { 
500                 rep_nop();
501                 barrier(); 
502         }
503
504         /* Set up for completion wait and then release other CPUs to change MTRRs*/
505         atomic_set (&undone_count, smp_num_cpus - 1);
506         wait_barrier_mtrr_disable = FALSE;
507         set_mtrr_disable (&ctxt);
508
509         /*  Wait for all other CPUs to flush and disable their caches  */
510         while (atomic_read (&undone_count) > 0) { 
511                 rep_nop ();
512                 barrier ();
513         }       
514
515         /* Set up for completion wait and then release other CPUs to change MTRRs */
516         atomic_set (&undone_count, smp_num_cpus - 1);
517         wait_barrier_execute = FALSE;
518         set_mtrr_up (reg, base, size, type, FALSE);
519
520         /*  Now wait for other CPUs to complete the function  */
521         while (atomic_read (&undone_count) > 0) {
522                 rep_nop();
523                 barrier ();
524         }       
525
526         /*  Now all CPUs should have finished the function. Release the barrier to
527            allow them to re-enable their caches and return from their interrupt,
528            then enable the local cache and return  */
529         wait_barrier_cache_enable = FALSE;
530         set_mtrr_done (&ctxt);
531 }
532
533
534 /*  Some BIOS's are fucked and don't set all MTRRs the same!  */
535 static void __init mtrr_state_warn (u32 mask)
536 {
537         if (!mask)
538                 return;
539         if (mask & MTRR_CHANGE_MASK_FIXED)
540                 printk (KERN_INFO "mtrr: your CPUs had inconsistent fixed MTRR settings\n");
541         if (mask & MTRR_CHANGE_MASK_VARIABLE)
542                 printk (KERN_INFO "mtrr: your CPUs had inconsistent variable MTRR settings\n");
543         if (mask & MTRR_CHANGE_MASK_DEFTYPE)
544                 printk (KERN_INFO "mtrr: your CPUs had inconsistent MTRRdefType settings\n");
545         printk (KERN_INFO "mtrr: probably your BIOS does not setup all CPUs\n");
546 }
547
548 #endif  /*  CONFIG_SMP  */
549
550
551 static inline char * attrib_to_str (int x)
552 {
553         return (x <= 6) ? mtrr_strings[x] : "?";
554 }
555
556
557 static void __init init_table (void)
558 {
559         int i, max;
560
561         max = get_num_var_ranges ();
562         if ((usage_table = kmalloc (max * sizeof *usage_table, GFP_KERNEL))==NULL) {
563                 printk ("mtrr: could not allocate\n");
564                 return;
565         }
566
567         for (i = 0; i < max; i++)
568                 usage_table[i] = 1;
569
570 #ifdef USERSPACE_INTERFACE
571         if ((ascii_buffer = kmalloc (max * LINE_SIZE, GFP_KERNEL)) == NULL) {
572                 printk ("mtrr: could not allocate\n");
573                 return;
574         }
575         ascii_buf_bytes = 0;
576         compute_ascii ();
577 #endif
578 }
579
580
581 /*
582  * Get a free MTRR.
583  * returns the index of the region on success, else -1 on error.
584 */
585 static int get_free_region(void)
586 {
587         int i, max;
588         mtrr_type ltype;
589         u64 lbase;
590         u32 lsize;
591
592         max = get_num_var_ranges ();
593         for (i = 0; i < max; ++i) {
594                 get_mtrr (i, &lbase, &lsize, &ltype);
595                 if (lsize == 0)
596                         return i;
597         }
598         return -ENOSPC;
599 }
600
601
602 /**
603  *      mtrr_add_page - Add a memory type region
604  *      @base: Physical base address of region in pages (4 KB)
605  *      @size: Physical size of region in pages (4 KB)
606  *      @type: Type of MTRR desired
607  *      @increment: If this is true do usage counting on the region
608  *      Returns The MTRR register on success, else a negative number
609  *      indicating the error code.
610  *
611  *      Memory type region registers control the caching on newer
612  *      processors. This function allows drivers to request an MTRR is added.
613  *      The caller should expect to need to provide a power of two size on
614  *      an equivalent power of two boundary.
615  *
616  *      If the region cannot be added either because all regions are in use
617  *      or the CPU cannot support it a negative value is returned. On success
618  *      the register number for this entry is returned, but should be treated
619  *      as a cookie only.
620  *
621  *      On a multiprocessor machine the changes are made to all processors.
622  *
623  *      The available types are
624  *
625  *      %MTRR_TYPE_UNCACHABLE   -       No caching
626  *      %MTRR_TYPE_WRBACK       -       Write data back in bursts whenever
627  *      %MTRR_TYPE_WRCOMB       -       Write data back soon but allow bursts
628  *      %MTRR_TYPE_WRTHROUGH    -       Cache reads but not writes
629  *
630  *      BUGS: Needs a quiet flag for the cases where drivers do not mind
631  *      failures and do not wish system log messages to be sent.
632  */
633
634 int mtrr_add_page (u64 base, u32 size, unsigned int type, char increment)
635 {
636         int i, max;
637         mtrr_type ltype;
638         u64 lbase, last;
639         u32 lsize;
640
641         if (base + size < 0x100) {
642                 printk (KERN_WARNING
643                         "mtrr: cannot set region below 1 MiB (0x%Lx000,0x%x000)\n",
644                         base, size);
645                 return -EINVAL;
646         }
647
648 #if 0 && defined(__x86_64__) && defined(CONFIG_AGP) 
649         {
650         agp_kern_info info; 
651         if (type != MTRR_TYPE_UNCACHABLE && agp_copy_info(&info) >= 0 && 
652             base<<PAGE_SHIFT >= info.aper_base && 
653             (base<<PAGE_SHIFT)+(size<<PAGE_SHIFT) >= 
654                         info.aper_base+info.aper_size*1024*1024)
655                 printk(KERN_INFO "%s[%d] setting conflicting mtrr into agp aperture\n",current->comm,current->pid); 
656         }
657 #endif
658
659         /*  Check upper bits of base and last are equal and lower bits are 0
660            for base and 1 for last  */
661         last = base + size - 1;
662         for (lbase = base; !(lbase & 1) && (last & 1);
663              lbase = lbase >> 1, last = last >> 1) ;
664
665         if (lbase != last) {
666                 printk (KERN_WARNING
667                         "mtrr: base(0x%Lx000) is not aligned on a size(0x%x000) boundary\n",
668                         base, size);
669                 return -EINVAL;
670         }
671
672         if (type >= MTRR_NUM_TYPES) {
673                 printk ("mtrr: type: %u illegal\n", type);
674                 return -EINVAL;
675         }
676
677         /*  If the type is WC, check that this processor supports it  */
678         if ((type == MTRR_TYPE_WRCOMB) && !have_wrcomb()) {
679                 printk (KERN_WARNING
680                         "mtrr: your processor doesn't support write-combining\n");
681                 return -ENOSYS;
682         }
683
684         if (base & size_or_mask) {
685                 printk (KERN_WARNING "mtrr: base(%Lx) exceeds the MTRR width(%Lx)\n",
686                                 base, size_or_mask);
687                 return -EINVAL;
688         }
689
690         if (size & size_or_mask) {
691                 printk (KERN_WARNING "mtrr: size exceeds the MTRR width\n");
692                 return -EINVAL;
693         }
694
695         increment = increment ? 1 : 0;
696         max = get_num_var_ranges ();
697         /*  Search for existing MTRR  */
698         down (&mtrr_lock);
699         for (i = 0; i < max; ++i) {
700                 get_mtrr (i, &lbase, &lsize, &ltype);
701                 if (base >= lbase + lsize)
702                         continue;
703                 if ((base < lbase) && (base + size <= lbase))
704                         continue;
705
706                 /*  At this point we know there is some kind of overlap/enclosure  */
707                 if ((base < lbase) || (base + size > lbase + lsize)) {
708                         up (&mtrr_lock);
709                         printk (KERN_WARNING
710                                 "mtrr: 0x%Lx000,0x%x000 overlaps existing"
711                                 " 0x%Lx000,0x%x000\n", base, size, lbase, lsize);
712                         return -EINVAL;
713                 }
714                 /*  New region is enclosed by an existing region  */
715                 if (ltype != type) {
716                         if (type == MTRR_TYPE_UNCACHABLE)
717                                 continue;
718                         up (&mtrr_lock);
719                         printk
720                             ("mtrr: type mismatch for %Lx000,%x000 old: %s new: %s\n",
721                              base, size,
722                                  attrib_to_str (ltype),
723                              attrib_to_str (type));
724                         return -EINVAL;
725                 }
726                 if (increment)
727                         ++usage_table[i];
728                 compute_ascii ();
729                 up (&mtrr_lock);
730                 return i;
731         }
732         /*  Search for an empty MTRR  */
733         i = get_free_region();
734         if (i < 0) {
735                 up (&mtrr_lock);
736                 printk ("mtrr: no more MTRRs available\n");
737                 return i;
738         }
739         set_mtrr (i, base, size, type);
740         usage_table[i] = 1;
741         compute_ascii ();
742         up (&mtrr_lock);
743         return i;
744 }
745
746
747 /**
748  *      mtrr_add - Add a memory type region
749  *      @base: Physical base address of region
750  *      @size: Physical size of region
751  *      @type: Type of MTRR desired
752  *      @increment: If this is true do usage counting on the region
753  *      Return the MTRR register on success, else a negative numbe
754  *      indicating the error code.
755  *
756  *      Memory type region registers control the caching on newer processors.
757  *      This function allows drivers to request an MTRR is added.
758  *      The caller should expect to need to provide a power of two size on
759  *      an equivalent power of two boundary.
760  *
761  *      If the region cannot be added either because all regions are in use
762  *      or the CPU cannot support it a negative value is returned. On success
763  *      the register number for this entry is returned, but should be treated
764  *      as a cookie only.
765  *
766  *      On a multiprocessor machine the changes are made to all processors.
767  *      This is required on x86 by the Intel processors.
768  *
769  *      The available types are
770  *
771  *      %MTRR_TYPE_UNCACHABLE   -       No caching
772  *      %MTRR_TYPE_WRBACK       -       Write data back in bursts whenever
773  *      %MTRR_TYPE_WRCOMB       -       Write data back soon but allow bursts
774  *      %MTRR_TYPE_WRTHROUGH    -       Cache reads but not writes
775  *
776  *      BUGS: Needs a quiet flag for the cases where drivers do not mind
777  *      failures and do not wish system log messages to be sent.
778  */
779
780 int mtrr_add (u64 base, u32 size, unsigned int type, char increment)
781 {
782         if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
783                 printk ("mtrr: size and base must be multiples of 4 kiB\n");
784                 printk ("mtrr: size: 0x%x  base: 0x%Lx\n", size, base);
785                 return -EINVAL;
786         }
787         return mtrr_add_page (base >> PAGE_SHIFT, size >> PAGE_SHIFT, type,
788                               increment);
789 }
790
791
792 /**
793  *      mtrr_del_page - delete a memory type region
794  *      @reg: Register returned by mtrr_add
795  *      @base: Physical base address
796  *      @size: Size of region
797  *
798  *      If register is supplied then base and size are ignored. This is
799  *      how drivers should call it.
800  *
801  *      Releases an MTRR region. If the usage count drops to zero the 
802  *      register is freed and the region returns to default state.
803  *      On success the register is returned, on failure a negative error
804  *      code.
805  */
806
807 int mtrr_del_page (int reg, u64 base, u32 size)
808 {
809         int i, max;
810         mtrr_type ltype;
811         u64 lbase;
812         u32 lsize;
813
814         max = get_num_var_ranges ();
815         down (&mtrr_lock);
816         if (reg < 0) {
817                 /*  Search for existing MTRR  */
818                 for (i = 0; i < max; ++i) {
819                         get_mtrr (i, &lbase, &lsize, &ltype);
820                         if (lbase == base && lsize == size) {
821                                 reg = i;
822                                 break;
823                         }
824                 }
825                 if (reg < 0) {
826                         up (&mtrr_lock);
827                         printk ("mtrr: no MTRR for %Lx000,%x000 found\n", base, size);
828                         return -EINVAL;
829                 }
830         }
831
832         if (reg >= max) {
833                 up (&mtrr_lock);
834                 printk ("mtrr: register: %d too big\n", reg);
835                 return -EINVAL;
836         }
837         get_mtrr (reg, &lbase, &lsize, &ltype);
838
839         if (lsize < 1) {
840                 up (&mtrr_lock);
841                 printk ("mtrr: MTRR %d not used\n", reg);
842                 return -EINVAL;
843         }
844
845         if (usage_table[reg] < 1) {
846                 up (&mtrr_lock);
847                 printk ("mtrr: reg: %d has count=0\n", reg);
848                 return -EINVAL;
849         }
850
851         if (--usage_table[reg] < 1)
852                 set_mtrr (reg, 0, 0, 0);
853         compute_ascii ();
854         up (&mtrr_lock);
855         return reg;
856 }
857
858
859 /**
860  *      mtrr_del - delete a memory type region
861  *      @reg: Register returned by mtrr_add
862  *      @base: Physical base address
863  *      @size: Size of region
864  *
865  *      If register is supplied then base and size are ignored. This is
866  *      how drivers should call it.
867  *
868  *      Releases an MTRR region. If the usage count drops to zero the 
869  *      register is freed and the region returns to default state.
870  *      On success the register is returned, on failure a negative error
871  *      code.
872  */
873
874 int mtrr_del (int reg, u64 base, u32 size)
875 {
876         if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
877                 printk ("mtrr: size and base must be multiples of 4 kiB\n");
878                 printk ("mtrr: size: 0x%x  base: 0x%Lx\n", size, base);
879                 return -EINVAL;
880         }
881         return mtrr_del_page (reg, base >> PAGE_SHIFT, size >> PAGE_SHIFT);
882 }
883
884
885 #ifdef USERSPACE_INTERFACE
886
887 static int mtrr_file_add (u64 base, u32 size, unsigned int type,
888                 struct file *file, int page)
889 {
890         int reg, max;
891         unsigned int *fcount = file->private_data;
892
893         max = get_num_var_ranges ();
894         if (fcount == NULL) {
895                 if ((fcount =
896                      kmalloc (max * sizeof *fcount, GFP_KERNEL)) == NULL) {
897                         printk ("mtrr: could not allocate\n");
898                         return -ENOMEM;
899                 }
900                 memset (fcount, 0, max * sizeof *fcount);
901                 file->private_data = fcount;
902         }
903
904         if (!page) {
905                 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
906                         printk
907                             (KERN_INFO "mtrr: size and base must be multiples of 4 kiB\n");
908                         printk (KERN_INFO "mtrr: size: 0x%x  base: 0x%Lx\n", size, base);
909                         return -EINVAL;
910                 }
911                 base >>= PAGE_SHIFT;
912                 size >>= PAGE_SHIFT;
913         }
914
915         reg = mtrr_add_page (base, size, type, 1);
916
917         if (reg >= 0)
918                 ++fcount[reg];
919         return reg;
920 }
921
922
923 static int mtrr_file_del (u64 base, u32 size,
924                 struct file *file, int page)
925 {
926         int reg;
927         unsigned int *fcount = file->private_data;
928
929         if (!page) {
930                 if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1))) {
931                         printk
932                             (KERN_INFO "mtrr: size and base must be multiples of 4 kiB\n");
933                         printk (KERN_INFO "mtrr: size: 0x%x  base: 0x%Lx\n", size, base);
934                         return -EINVAL;
935                 }
936                 base >>= PAGE_SHIFT;
937                 size >>= PAGE_SHIFT;
938         }
939         reg = mtrr_del_page (-1, base, size);
940         if (reg < 0)
941                 return reg;
942         if (fcount == NULL)
943                 return reg;
944         if (fcount[reg] < 1)
945                 return -EINVAL;
946         --fcount[reg];
947         return reg;
948 }
949
950
951 static ssize_t mtrr_read (struct file *file, char *buf, size_t len,
952                 loff_t * ppos)
953 {
954         loff_t n = *ppos;
955         unsigned pos = n;
956
957         if (pos != n || pos >= ascii_buf_bytes)
958                 return 0;
959
960         if (len > ascii_buf_bytes - pos)
961                 len = ascii_buf_bytes - pos;
962
963         if (copy_to_user (buf, ascii_buffer + pos, len))
964                 return -EFAULT;
965
966         *ppos = pos + len;
967         return len;
968 }
969
970
971 static ssize_t mtrr_write (struct file *file, const char *buf,
972                 size_t len, loff_t * ppos)
973 /*  Format of control line:
974     "base=%Lx size=%Lx type=%s"     OR:
975     "disable=%d"
976 */
977 {
978         int i, err, reg;
979         u64 base;
980         u32 size;
981         char *ptr;
982         char line[LINE_SIZE];
983
984         if (!capable(CAP_SYS_ADMIN))
985                 return -EPERM;
986
987         /*  Can't seek (pwrite) on this device  */
988         if (ppos != &file->f_pos)
989                 return -ESPIPE;
990         memset (line, 0, LINE_SIZE);
991
992         if (len > LINE_SIZE)
993                 len = LINE_SIZE;
994
995         if (copy_from_user (line, buf, len - 1))
996                 return -EFAULT;
997         ptr = line + strlen (line) - 1;
998
999         if (*ptr == '\n')
1000                 *ptr = '\0';
1001
1002         if (!strncmp (line, "disable=", 8)) {
1003                 reg = simple_strtoul (line + 8, &ptr, 0);
1004                 err = mtrr_del_page (reg, 0, 0);
1005                 if (err < 0)
1006                         return err;
1007                 return len;
1008         }
1009
1010         if (strncmp (line, "base=", 5)) {
1011                 printk (KERN_INFO "mtrr: no \"base=\" in line: \"%s\"\n", line);
1012                 return -EINVAL;
1013         }
1014
1015         base = simple_strtoull (line + 5, &ptr, 0);
1016
1017         for (; isspace (*ptr); ++ptr) ;
1018
1019         if (strncmp (ptr, "size=", 5)) {
1020                 printk (KERN_INFO "mtrr: no \"size=\" in line: \"%s\"\n", line);
1021                 return -EINVAL;
1022         }
1023
1024         size = simple_strtoull (ptr + 5, &ptr, 0);
1025
1026         if ((base & 0xfff) || (size & 0xfff)) {
1027                 printk (KERN_INFO "mtrr: size and base must be multiples of 4 kiB\n");
1028                 printk (KERN_INFO "mtrr: size: 0x%x  base: 0x%Lx\n", size, base);
1029                 return -EINVAL;
1030         }
1031
1032         for (; isspace (*ptr); ++ptr) ;
1033
1034         if (strncmp (ptr, "type=", 5)) {
1035                 printk (KERN_INFO "mtrr: no \"type=\" in line: \"%s\"\n", line);
1036                 return -EINVAL;
1037         }
1038         ptr += 5;
1039
1040         for (; isspace (*ptr); ++ptr) ;
1041
1042         for (i = 0; i < MTRR_NUM_TYPES; ++i) {
1043                 if (strcmp (ptr, mtrr_strings[i]))
1044                         continue;
1045                 base >>= PAGE_SHIFT;
1046                 size >>= PAGE_SHIFT;
1047                 err = mtrr_add_page ((u64) base, size, i, 1);
1048                 if (err < 0)
1049                         return err;
1050                 return len;
1051         }
1052         printk (KERN_INFO "mtrr: illegal type: \"%s\"\n", ptr);
1053         return -EINVAL;
1054 }
1055
1056
1057 static int mtrr_ioctl (struct inode *inode, struct file *file,
1058                 unsigned int cmd, unsigned long arg)
1059 {
1060         int err;
1061         mtrr_type type;
1062         struct mtrr_sentry sentry;
1063         struct mtrr_gentry gentry;
1064
1065         switch (cmd) {
1066         default:
1067                 return -ENOIOCTLCMD;
1068
1069         case MTRRIOC_ADD_ENTRY:
1070                 if (!capable(CAP_SYS_ADMIN))
1071                         return -EPERM;
1072                 if (copy_from_user (&sentry, (void *) arg, sizeof sentry))
1073                         return -EFAULT;
1074                 err = mtrr_file_add (sentry.base, sentry.size, sentry.type,
1075                                    file, 0);
1076                 if (err < 0)
1077                         return err;
1078                 break;
1079
1080         case MTRRIOC_SET_ENTRY:
1081                 if (!capable(CAP_SYS_ADMIN))
1082                         return -EPERM;
1083                 if (copy_from_user (&sentry, (void *) arg, sizeof sentry))
1084                         return -EFAULT;
1085                 err = mtrr_add (sentry.base, sentry.size, sentry.type, 0);
1086                 if (err < 0)
1087                         return err;
1088                 break;
1089
1090         case MTRRIOC_DEL_ENTRY:
1091                 if (!capable(CAP_SYS_ADMIN))
1092                         return -EPERM;
1093                 if (copy_from_user (&sentry, (void *) arg, sizeof sentry))
1094                         return -EFAULT;
1095                 err = mtrr_file_del (sentry.base, sentry.size, file, 0);
1096                 if (err < 0)
1097                         return err;
1098                 break;
1099
1100         case MTRRIOC_KILL_ENTRY:
1101                 if (!capable(CAP_SYS_ADMIN))
1102                         return -EPERM;
1103                 if (copy_from_user (&sentry, (void *) arg, sizeof sentry))
1104                         return -EFAULT;
1105                 err = mtrr_del (-1, sentry.base, sentry.size);
1106                 if (err < 0)
1107                         return err;
1108                 break;
1109
1110         case MTRRIOC_GET_ENTRY:
1111                 if (copy_from_user (&gentry, (void *) arg, sizeof gentry))
1112                         return -EFAULT;
1113                 if (gentry.regnum >= get_num_var_ranges ())
1114                         return -EINVAL;
1115                 get_mtrr (gentry.regnum, (u64*) &gentry.base, &gentry.size, &type);
1116
1117                 /* Hide entries that go above 4GB */
1118                 if (gentry.base + gentry.size > 0x100000
1119                     || gentry.size == 0x100000)
1120                         gentry.base = gentry.size = gentry.type = 0;
1121                 else {
1122                         gentry.base <<= PAGE_SHIFT;
1123                         gentry.size <<= PAGE_SHIFT;
1124                         gentry.type = type;
1125                 }
1126
1127                 if (copy_to_user ((void *) arg, &gentry, sizeof gentry))
1128                         return -EFAULT;
1129                 break;
1130
1131         case MTRRIOC_ADD_PAGE_ENTRY:
1132                 if (!capable(CAP_SYS_ADMIN))
1133                         return -EPERM;
1134                 if (copy_from_user (&sentry, (void *) arg, sizeof sentry))
1135                         return -EFAULT;
1136                 err = mtrr_file_add (sentry.base, sentry.size, sentry.type, file, 1);
1137                 if (err < 0)
1138                         return err;
1139                 break;
1140
1141         case MTRRIOC_SET_PAGE_ENTRY:
1142                 if (!capable(CAP_SYS_ADMIN))
1143                         return -EPERM;
1144                 if (copy_from_user (&sentry, (void *) arg, sizeof sentry))
1145                         return -EFAULT;
1146                 err = mtrr_add_page (sentry.base, sentry.size, sentry.type, 0);
1147                 if (err < 0)
1148                         return err;
1149                 break;
1150
1151         case MTRRIOC_DEL_PAGE_ENTRY:
1152                 if (!capable(CAP_SYS_ADMIN))
1153                         return -EPERM;
1154                 if (copy_from_user (&sentry, (void *) arg, sizeof sentry))
1155                         return -EFAULT;
1156                 err = mtrr_file_del (sentry.base, sentry.size, file, 1);
1157                 if (err < 0)
1158                         return err;
1159                 break;
1160
1161         case MTRRIOC_KILL_PAGE_ENTRY:
1162                 if (!capable(CAP_SYS_ADMIN))
1163                         return -EPERM;
1164                 if (copy_from_user (&sentry, (void *) arg, sizeof sentry))
1165                         return -EFAULT;
1166                 err = mtrr_del_page (-1, sentry.base, sentry.size);
1167                 if (err < 0)
1168                         return err;
1169                 break;
1170
1171         case MTRRIOC_GET_PAGE_ENTRY:
1172                 if (copy_from_user (&gentry, (void *) arg, sizeof gentry))
1173                         return -EFAULT;
1174                 if (gentry.regnum >= get_num_var_ranges ())
1175                         return -EINVAL;
1176                 get_mtrr (gentry.regnum, (u64*) &gentry.base, &gentry.size, &type);
1177                 gentry.type = type;
1178
1179                 if (copy_to_user ((void *) arg, &gentry, sizeof gentry))
1180                         return -EFAULT;
1181                 break;
1182         }
1183         return 0;
1184 }
1185
1186
1187 static int mtrr_close (struct inode *ino, struct file *file)
1188 {
1189         int i, max;
1190         unsigned int *fcount = file->private_data;
1191
1192         if (fcount == NULL)
1193                 return 0;
1194
1195         lock_kernel ();
1196         max = get_num_var_ranges ();
1197         for (i = 0; i < max; ++i) {
1198                 while (fcount[i] > 0) {
1199                         if (mtrr_del (i, 0, 0) < 0)
1200                                 printk ("mtrr: reg %d not used\n", i);
1201                         --fcount[i];
1202                 }
1203         }
1204         unlock_kernel ();
1205         kfree (fcount);
1206         file->private_data = NULL;
1207         return 0;
1208 }
1209
1210
1211 static struct file_operations mtrr_fops = {
1212         owner:  THIS_MODULE,
1213         read:   mtrr_read,
1214         write:  mtrr_write,
1215         ioctl:  mtrr_ioctl,
1216         release:mtrr_close,
1217 };
1218
1219 #ifdef CONFIG_PROC_FS
1220 static struct proc_dir_entry *proc_root_mtrr;
1221 #endif
1222
1223 static devfs_handle_t devfs_handle;
1224
1225 static void compute_ascii (void)
1226 {
1227         char factor;
1228         int i, max;
1229         mtrr_type type;
1230         u64 base;
1231         u32 size;
1232
1233         ascii_buf_bytes = 0;
1234         max = get_num_var_ranges ();
1235         for (i = 0; i < max; i++) {
1236                 get_mtrr (i, &base, &size, &type);
1237                 if (size == 0)
1238                         usage_table[i] = 0;
1239                 else {
1240                         if (size < (0x100000 >> PAGE_SHIFT)) {
1241                                 /* less than 1MB */
1242                                 factor = 'K';
1243                                 size <<= PAGE_SHIFT - 10;
1244                         } else {
1245                                 factor = 'M';
1246                                 size >>= 20 - PAGE_SHIFT;
1247                         }
1248                         sprintf (ascii_buffer + ascii_buf_bytes,
1249                                 "reg%02i: base=0x%05Lx000 (%4iMB), size=%4i%cB: %s, count=%d\n",
1250                                 i, base, (u32) base >> (20 - PAGE_SHIFT), size, factor,
1251                                 attrib_to_str (type), usage_table[i]);
1252                         ascii_buf_bytes += strlen (ascii_buffer + ascii_buf_bytes);
1253                 }
1254         }
1255         devfs_set_file_size (devfs_handle, ascii_buf_bytes);
1256 #ifdef CONFIG_PROC_FS
1257         if (proc_root_mtrr)
1258                 proc_root_mtrr->size = ascii_buf_bytes;
1259 #endif
1260 }
1261
1262 #endif  /*  USERSPACE_INTERFACE  */
1263
1264 EXPORT_SYMBOL (mtrr_add);
1265 EXPORT_SYMBOL (mtrr_del);
1266
1267
1268 static void __init mtrr_setup (void)
1269 {
1270         printk ("mtrr: v%s)\n", MTRR_VERSION);
1271
1272         if (test_bit (X86_FEATURE_MTRR, boot_cpu_data.x86_capability)) {
1273                 /* Query the width (in bits) of the physical
1274                    addressable memory. This is an AMD specific MSR,
1275                    but we assume(hope?) Intel will implement it too
1276                    when they extend the width of the Xeon address bus. */
1277                 if (cpuid_eax (0x80000000) >= 0x80000008) {
1278                         u32 phys_addr;
1279                         phys_addr = cpuid_eax (0x80000008) & 0xff;
1280                         size_or_mask = ~((1L << (phys_addr - PAGE_SHIFT)) - 1);
1281                         /*
1282                          * top bits MBZ as its beyond the addressable range.
1283                          * bottom bits MBZ as we don't care about lower 12 bits of addr.
1284                          */
1285                         size_and_mask = ~size_or_mask &  0xfff00000;
1286                 } else {
1287                         /* 36bit fallback */
1288                         size_or_mask = 0xff000000;
1289                         size_and_mask = 0x00f00000;
1290                 }
1291         }
1292 }
1293
1294 #ifdef CONFIG_SMP
1295
1296 static volatile u32 smp_changes_mask __initdata = 0;
1297 static struct mtrr_state smp_mtrr_state __initdata = { 0, 0 };
1298
1299 void __init mtrr_init_boot_cpu (void)
1300 {
1301         mtrr_setup();
1302         get_mtrr_state (&smp_mtrr_state);
1303 }
1304
1305
1306 void __init mtrr_init_secondary_cpu (void)
1307 {
1308         u64 mask;
1309         int count;
1310         struct set_mtrr_context ctxt;
1311
1312         /* Note that this is not ideal, since the cache is only flushed/disabled
1313            for this CPU while the MTRRs are changed, but changing this requires
1314            more invasive changes to the way the kernel boots  */
1315         set_mtrr_prepare (&ctxt);
1316         set_mtrr_disable (&ctxt);
1317         mask = set_mtrr_state (&smp_mtrr_state, &ctxt);
1318         set_mtrr_done (&ctxt);
1319
1320         /*  Use the atomic bitops to update the global mask  */
1321         for (count = 0; count < sizeof mask * 8; ++count) {
1322                 if (mask & 0x01)
1323                         set_bit (count, &smp_changes_mask);
1324                 mask >>= 1;
1325         }
1326 }
1327
1328 #endif  /*  CONFIG_SMP  */
1329
1330
1331 int __init mtrr_init (void)
1332 {
1333 #ifdef CONFIG_SMP
1334         /* mtrr_setup() should already have been called from mtrr_init_boot_cpu() */
1335
1336         finalize_mtrr_state (&smp_mtrr_state);
1337         mtrr_state_warn (smp_changes_mask);
1338 #else
1339         mtrr_setup();
1340 #endif
1341
1342 #ifdef CONFIG_PROC_FS
1343         proc_root_mtrr = create_proc_entry ("mtrr", S_IWUSR | S_IRUGO, &proc_root);
1344         if (proc_root_mtrr) {
1345                 proc_root_mtrr->owner = THIS_MODULE;
1346                 proc_root_mtrr->proc_fops = &mtrr_fops;
1347         }
1348 #endif
1349 #ifdef CONFIG_DEVFS_FS
1350         devfs_handle = devfs_register (NULL, "cpu/mtrr", DEVFS_FL_DEFAULT, 0, 0,
1351                                 S_IFREG | S_IRUGO | S_IWUSR,
1352                                 &mtrr_fops, NULL);
1353 #endif
1354         init_table ();
1355         return 0;
1356 }