mtrr.txt 10 KB

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