- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / Documentation / mtrr.txt
blob01332836346a51778e38a9416460136bf1582123
1 MTRR (Memory Type Range Register) control
2 3 Jun 1999
3 Richard Gooch
4 <rgooch@atnf.csiro.au>
6   On Intel P6 family processors (Pentium Pro, Pentium II and later)
7   the Memory Type Range Registers (MTRRs) may be used to control
8   processor access to memory ranges. This is most useful when you have
9   a video (VGA) card on a PCI or AGP bus. Enabling write-combining
10   allows bus write transfers to be combined into a larger transfer
11   before bursting over the PCI/AGP bus. This can increase performance
12   of image write operations 2.5 times or more.
14   The Cyrix 6x86, 6x86MX and M II processors have Address Range
15   Registers (ARRs) which provide a similar functionality to MTRRs. For
16   these, the ARRs are used to emulate the MTRRs.
18   The AMD K6-2 (stepping 8 and above) and K6-3 processors have two
19   MTRRs. These are supported.
20   
21   The Centaur C6 (WinChip) has 8 MCRs, allowing write-combining. These
22   are supported.
24   The CONFIG_MTRR option creates a /proc/mtrr file which may be used
25   to manipulate your MTRRs. Typically the X server should use
26   this. This should have a reasonably generic interface so that
27   similar control registers on other processors can be easily
28   supported.
31 There are two interfaces to /proc/mtrr: one is an ASCII interface
32 which allows you to read and write. The other is an ioctl()
33 interface. The ASCII interface is meant for administration. The
34 ioctl() interface is meant for C programs (i.e. the X server). The
35 interfaces are described below, with sample commands and C code.
37 ===============================================================================
38 Reading MTRRs from the shell:
40 % cat /proc/mtrr
41 reg00: base=0x00000000 (   0MB), size= 128MB: write-back, count=1
42 reg01: base=0x08000000 ( 128MB), size=  64MB: write-back, count=1
43 ===============================================================================
44 Creating MTRRs from the C-shell:
45 # echo "base=0xf8000000 size=0x400000 type=write-combining" >! /proc/mtrr
46 or if you use bash:
47 # echo "base=0xf8000000 size=0x400000 type=write-combining" >| /proc/mtrr
49 And the result thereof:
50 % cat /proc/mtrr
51 reg00: base=0x00000000 (   0MB), size= 128MB: write-back, count=1
52 reg01: base=0x08000000 ( 128MB), size=  64MB: write-back, count=1
53 reg02: base=0xf8000000 (3968MB), size=   4MB: write-combining, count=1
55 This is for video RAM at base address 0xf8000000 and size 4 megabytes. To
56 find out your base address, you need to look at the output of your X
57 server, which tells you where the linear framebuffer address is. A
58 typical line that you may get is:
60 (--) S3: PCI: 968 rev 0, Linear FB @ 0xf8000000
62 Note that you should only use the value from the X server, as it may
63 move the framebuffer base address, so the only value you can trust is
64 that reported by the X server.
66 To find out the size of your framebuffer (what, you don't actually
67 know?), the following line will tell you:
69 (--) S3: videoram:  4096k
71 That's 4 megabytes, which is 0x400000 bytes (in hexadecimal).
72 A patch is being written for XFree86 which will make this automatic:
73 in other words the X server will manipulate /proc/mtrr using the
74 ioctl() interface, so users won't have to do anything. If you use a
75 commercial X server, lobby your vendor to add support for MTRRs.
76 ===============================================================================
77 Creating overlapping MTRRs:
79 %echo "base=0xfb000000 size=0x1000000 type=write-combining" >/proc/mtrr
80 %echo "base=0xfb000000 size=0x1000 type=uncachable" >/proc/mtrr
82 And the results: cat /proc/mtrr
83 reg00: base=0x00000000 (   0MB), size=  64MB: write-back, count=1
84 reg01: base=0xfb000000 (4016MB), size=  16MB: write-combining, count=1
85 reg02: base=0xfb000000 (4016MB), size=   4kB: uncachable, count=1
87 Some cards (especially Voodoo Graphics boards) need this 4 kB area 
88 excluded from the beginning of the region because it is used for
89 registers.
91 NOTE: You can only create type=uncachable region, if the first
92 region that you created is type=write-combining.
93 ===============================================================================
94 Removing MTRRs from the C-shell:
95 % echo "disable=2" >! /proc/mtrr
96 or using bash:
97 % echo "disable=2" >| /proc/mtrr
98 ===============================================================================
99 Reading MTRRs from a C program using ioctl()'s:
101 /*  mtrr-show.c
103     Source file for mtrr-show (example program to show MTRRs using ioctl()'s)
105     Copyright (C) 1997-1998  Richard Gooch
107     This program is free software; you can redistribute it and/or modify
108     it under the terms of the GNU General Public License as published by
109     the Free Software Foundation; either version 2 of the License, or
110     (at your option) any later version.
112     This program is distributed in the hope that it will be useful,
113     but WITHOUT ANY WARRANTY; without even the implied warranty of
114     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
115     GNU General Public License for more details.
117     You should have received a copy of the GNU General Public License
118     along with this program; if not, write to the Free Software
119     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
121     Richard Gooch may be reached by email at  rgooch@atnf.csiro.au
122     The postal address is:
123       Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
127     This program will use an ioctl() on /proc/mtrr to show the current MTRR
128     settings. This is an alternative to reading /proc/mtrr.
131     Written by      Richard Gooch   17-DEC-1997
133     Last updated by Richard Gooch   2-MAY-1998
137 #include <stdio.h>
138 #include <string.h>
139 #include <sys/types.h>
140 #include <sys/stat.h>
141 #include <fcntl.h>
142 #include <sys/ioctl.h>
143 #include <errno.h>
144 #define MTRR_NEED_STRINGS
145 #include <asm/mtrr.h>
147 #define TRUE 1
148 #define FALSE 0
149 #define ERRSTRING strerror (errno)
152 int main ()
154     int fd;
155     struct mtrr_gentry gentry;
157     if ( ( fd = open ("/proc/mtrr", O_RDONLY, 0) ) == -1 )
158     {
159         if (errno == ENOENT)
160         {
161             fputs ("/proc/mtrr not found: not supported or you don't have a PPro?\n",
162                    stderr);
163             exit (1);
164         }
165         fprintf (stderr, "Error opening /proc/mtrr\t%s\n", ERRSTRING);
166         exit (2);
167     }
168     for (gentry.regnum = 0; ioctl (fd, MTRRIOC_GET_ENTRY, &gentry) == 0;
169          ++gentry.regnum)
170     {
171         if (gentry.size < 1)
172         {
173             fprintf (stderr, "Register: %u disabled\n", gentry.regnum);
174             continue;
175         }
176         fprintf (stderr, "Register: %u base: 0x%lx size: 0x%lx type: %s\n",
177                  gentry.regnum, gentry.base, gentry.size,
178                  mtrr_strings[gentry.type]);
179     }
180     if (errno == EINVAL) exit (0);
181     fprintf (stderr, "Error doing ioctl(2) on /dev/mtrr\t%s\n", ERRSTRING);
182     exit (3);
183 }   /*  End Function main  */
184 ===============================================================================
185 Creating MTRRs from a C programme using ioctl()'s:
187 /*  mtrr-add.c
189     Source file for mtrr-add (example programme to add an MTRRs using ioctl())
191     Copyright (C) 1997-1998  Richard Gooch
193     This program is free software; you can redistribute it and/or modify
194     it under the terms of the GNU General Public License as published by
195     the Free Software Foundation; either version 2 of the License, or
196     (at your option) any later version.
198     This program is distributed in the hope that it will be useful,
199     but WITHOUT ANY WARRANTY; without even the implied warranty of
200     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
201     GNU General Public License for more details.
203     You should have received a copy of the GNU General Public License
204     along with this program; if not, write to the Free Software
205     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
207     Richard Gooch may be reached by email at  rgooch@atnf.csiro.au
208     The postal address is:
209       Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
213     This programme will use an ioctl() on /proc/mtrr to add an entry. The first
214     available mtrr is used. This is an alternative to writing /proc/mtrr.
217     Written by      Richard Gooch   17-DEC-1997
219     Last updated by Richard Gooch   2-MAY-1998
223 #include <stdio.h>
224 #include <string.h>
225 #include <stdlib.h>
226 #include <unistd.h>
227 #include <sys/types.h>
228 #include <sys/stat.h>
229 #include <fcntl.h>
230 #include <sys/ioctl.h>
231 #include <errno.h>
232 #define MTRR_NEED_STRINGS
233 #include <asm/mtrr.h>
235 #define TRUE 1
236 #define FALSE 0
237 #define ERRSTRING strerror (errno)
240 int main (int argc, char **argv)
242     int fd;
243     struct mtrr_sentry sentry;
245     if (argc != 4)
246     {
247         fprintf (stderr, "Usage:\tmtrr-add base size type\n");
248         exit (1);
249     }
250     sentry.base = strtoul (argv[1], NULL, 0);
251     sentry.size = strtoul (argv[2], NULL, 0);
252     for (sentry.type = 0; sentry.type < MTRR_NUM_TYPES; ++sentry.type)
253     {
254         if (strcmp (argv[3], mtrr_strings[sentry.type]) == 0) break;
255     }
256     if (sentry.type >= MTRR_NUM_TYPES)
257     {
258         fprintf (stderr, "Illegal type: \"%s\"\n", argv[3]);
259         exit (2);
260     }
261     if ( ( fd = open ("/proc/mtrr", O_WRONLY, 0) ) == -1 )
262     {
263         if (errno == ENOENT)
264         {
265             fputs ("/proc/mtrr not found: not supported or you don't have a PPro?\n",
266                    stderr);
267             exit (3);
268         }
269         fprintf (stderr, "Error opening /proc/mtrr\t%s\n", ERRSTRING);
270         exit (4);
271     }
272     if (ioctl (fd, MTRRIOC_ADD_ENTRY, &sentry) == -1)
273     {
274         fprintf (stderr, "Error doing ioctl(2) on /dev/mtrr\t%s\n", ERRSTRING);
275         exit (5);
276     }
277     fprintf (stderr, "Sleeping for 5 seconds so you can see the new entry\n");
278     sleep (5);
279     close (fd);
280     fputs ("I've just closed /proc/mtrr so now the new entry should be gone\n",
281            stderr);
282 }   /*  End Function main  */
283 ===============================================================================