mtrr.txt 11 KB

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